我想要一个按钮,可以跳转到指定的网址并在新标签页中打开。如何实现这个功能?
使用这个:
<input type="button" value="button name" onclick="window.open('http://www.website.com/page')" />
这个方法适合我,它会打开一个真正的新“弹出”窗口,而不是一个新的完整浏览器或选项卡。您还可以向其中添加变量,以防止它显示特定的浏览器属性,如下所示:
onclick="window.open(this.href,'popUpWindow','height=400,width=600,left=10,top=10,,scrollbars=yes,menubar=no'); return false;"
window.open(url, "_blank");
添加target="_blank"应该就可以了:
<a id="myLink" href="www.google.com" target="_blank">google</a>
<button>
来代替<a>
作为链接。例如,你正在使用一个HTML模板,作者基于<button>
元素样式化了社交图标,如button.social-icon{ width:... }
。编辑作者的样式不是一个好主意,因为“更新”过程会让你痛苦不堪。 - Ahmad Maleki你可以忘记使用JavaScript,因为浏览器决定是否在新标签页中打开它。你最好的选择是使用以下方法:
<form action="http://www.yoursite.com/dosomething" method="get" target="_blank">
<input name="dynamicParam1" type="text"/>
<input name="dynamicParam2" type="text" />
<input type="submit" value="submit" />
</form>
无论客户端使用哪个浏览器,由于 target="_blank"
属性,此链接将始终在新标签页中打开。
如果您只需要重定向而没有动态参数,您可以像Tim Büthe建议的那样使用带有 target="_blank"
属性的链接。
我偏爱的方法有一个优点,那就是标记中没有嵌入JavaScript:
CSS
a {
color: inherit;
text-decoration: none;
}
HTML
<a href="http://example.com" target="_blank"><input type="button" value="Link-button"></a>
a input[type=button]
CSS 选择器,而不是仅使用 a
。 - RixTheTyrunt使用 window.open
而不是 window.location
来打开一个新的窗口或标签页(根据浏览器设置)。
你的演示无法工作,因为没有选择 button
元素。尝试使用 input[type=button]
或给按钮加上一个 id
并使用 #buttonId
。
使用javascript在新标签页中打开
<button onclick="window.open('https://www.our-url.com')" id="myButton"
class="btn request-callback" >Explore More </button>
我刚刚在表单标签下使用了target="_blank",在Firefox和Chrome中它可以在新标签页中打开,但在IE中会在新窗口中打开。
<a id="link" href="www.gmail.com" target="_blank" >gmail</a>
<button onClick={() => window.open(url, "_blank")}> content </button>
使用锚点标签:
<a href="url" target="_blank" > content </a>