Html 输入按钮 target="_blank" 不会导致链接在新窗口/选项卡中加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17207342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input button target="_blank" isn't causing the link to load in a new window/tab
提问by Teemu Laine
I have an HTML file with an input button. This is only an example button, but it's including all necessary info of code:
我有一个带有输入按钮的 HTML 文件。这只是一个示例按钮,但它包括所有必要的代码信息:
<input type="button" onClick="parent.location='http://www.facebook.com/'" value="facebook" target="_blank">
For some reason, it's not loading in a new window/tab.
出于某种原因,它没有加载到新窗口/选项卡中。
采纳答案by jycr753
you will need to do it like this...
你需要这样做......
<a type="button" href="http://www.facebook.com/" value="facebook" target="_blank" class="button"></a>
and add the basic css if you want it to look like a btn.. like this
如果你想让它看起来像一个 btn.. 像这样添加基本的 css
.button {
width:100px;
height:50px;
-moz-box-shadow:inset 0 1px 0 0 #fff;
-webkit-box-shadow:inset 0 1px 0 0 #fff;
box-shadow:inset 0 1px 0 0 #fff;
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ffffff), color-stop(1, #d1d1d1) );
background:-moz-linear-gradient( center top, #ffffff 5%, #d1d1d1 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#d1d1d1');
background-color:#fff;
-moz-border-radius:6px;
-webkit-border-radius:6px;
border-radius:6px;
border:1px solid #dcdcdc;
display:inline-block;
color:#777;
font-family:Helvetica;
font-size:15px;
font-weight:700;
padding:6px 24px;
text-decoration:none;
text-shadow:1px 1px 0 #fff
}
.button:hover {
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #d1d1d1), color-stop(1, #ffffff) );
background:-moz-linear-gradient( center top, #d1d1d1 5%, #ffffff 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#d1d1d1', endColorstr='#ffffff');
background-color:#d1d1d1
}
.button:active {
position:relative;
top:1px
}
target works only with href tags..
目标仅适用于 href 标签。
回答by mans
use formtarget="_blank"
its working for me
使用formtarget="_blank"
它为我工作
<input type="button" onClick="parent.location='http://www.facebook.com/'" value="facebook" formtarget="_blank">
Browser compatibility: from caniuse.com
IE: 10+
| Edge: 12+
| Firefox: 4+
| Chrome: 15+
| Safari/iOS: 5.1+
| Android: 4+
浏览器兼容性:来自caniuse.com
IE:10+ | 边缘:12+ | 火狐:4+ | 铬:15+ | Safari/iOS: 5.1+ | 安卓:4+
回答by Davis
The correct answer:
正确答案:
<form role="search" method="get" action="" target="_blank"></form>
Supported in all major browsers :)
所有主要浏览器都支持:)
回答by Gianluca Greco
Facing a similar problem, I solved it this way:
遇到类似的问题,我是这样解决的:
<a href="http://www.google.com/" target="_top" style="text-decoration:none"><button id="back">Back</button></a>
Change _topwith _blankif this is what you need.
如果这是您需要的,请将_top更改为_blank。
回答by Gianluca Greco
target
isn't valid on an input
element.
target
对input
元素无效。
In this case, though, your redirection is done by Javascript, so you could have your script open up a new window.
但是,在这种情况下,您的重定向是由 Javascript 完成的,因此您可以让脚本打开一个新窗口。
回答by Grizzly
The formtarget attribute is only used for buttons with type="submit".
formtarget 属性仅用于 type="submit" 的按钮。
That is from this reference.
那是从这个参考。
Here is an answer using JavaScript:
这是使用JavaScript的答案:
<input type="button" onClick="openNewTab()" value="facebook">
<script type="text/javascript">
function openNewTab() {
window.open("http://www.facebook.com/");
}
</script>
回答by MaVRoSCy
An input
element does not support the target
attribute. The target
attribute is for a
tags and that is where it should be used.
一个input
元素不支持的target
属性。该target
属性用于a
标记,这就是应该使用它的地方。
回答by nick
Instead of
代替
onClick="parent.location='http://www.facebook.com/'"
onClick="parent.location=' http://www.facebook.com/'"
try using
尝试使用
onClick="window.open='http://www.facebook.com/'" onClick="window.open('http://www.facebook.com/','facebook')"
onClick=" window.open=' http://www.facebook.com/'" onClick="window.open(' http://www.facebook.com/','facebook')"
回答by JoshYates1980
<form target="_blank">
<button class="btn btn-primary" formaction="http://www.google.com">Google</button>
</form>
回答by Saikat Das
Please try this it's working for me
请试试这个它对我有用
onClick="window.open('http://www.facebook.com/','facebook')"
onClick=" window.open(' http://www.facebook.com/','facebook')"
<button type="button" class="btn btn-default btn-social" onClick="window.open('http://www.facebook.com/','facebook')">
<i class="fa fa-facebook" aria-hidden="true"></i>
</button>