Javascript 在新窗口中打开按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4643958/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:24:54  来源:igfitidea点击:

Open button in new window?

javascripthtmlbuttonwindownew-operator

提问by Latox

How would I go about making the button open in a new window, emulating "a href, target = _blank"?

我将如何在新窗口中打开按钮,模拟“a href,target = _blank”?

I currently have:

我目前有:

<button class="button" onClick="window.location.href='http://www.example.com';"> 
     <span class="icon">Open</span>
</button>

The button isn't in a form, I just want to make it open in a new window.

该按钮不在窗体中,我只想在新窗口中打开它。

回答by Damien-Wright

Opens a new window with the url you supplied :)

使用您提供的网址打开一个新窗口:)

<button class="button" onClick="window.open('http://www.example.com');">
     <span class="icon">Open</span>
</button>

hope that helps :)

希望有帮助:)

回答by josh.thomson

I couldn't get your method to work @Damien-at-SF...

我无法让您的方法起作用@Damien-at-SF ...

So I resorted to my old knowledge.

所以我求助于我的旧知识。

By encasing the input type="button" within a hyperlink element, you can simply declare the target property as so:

通过将 input type="button" 封装在超链接元素中,您可以简单地声明目标属性,如下所示:

<a href="http://www.site.org" target="_blank">
<input type="button" class="button" value="Open" />
</a>

The 'target="_blank"' is the property which makes the browser open the link within a new tab. This attribute has other properties, See: http://www.w3schools.com/tags/att_a_target.aspfor further details.

'target="_blank"' 是使浏览器在新选项卡中打开链接的属性。此属性还有其他属性,请参阅:http: //www.w3schools.com/tags/att_a_target.asp了解更多详情。

Since the 'value=""' attribute on buttons will write the contained string to the button, a span is not necessary.

由于按钮上的 'value=""' 属性会将包含的字符串写入按钮,因此不需要跨度。

Instead of writing:

而不是写:

<element></element>

for most HTML elements you can simply close them with a trailing slash, like so:

对于大多数 HTML 元素,您可以简单地用斜杠关闭它们,如下所示:

<element />

Oh, and finally... a 'button' element has a refresh trigger within it, so I use an 'input type[button]' to avoid triggering the form.

哦,最后......一个'button'元素在它里面有一个刷新触发器,所以我使用一个'input type[button]'来避免触发表单。

Good Luck Programmers.

祝程序员好运。

Due to StackOverflow's policy I had to change the domain in the example: https://meta.stackexchange.com/questions/208963/why-are-certain-example-urls-like-http-site-com-and-http-mysite-com-blocke

由于 StackOverflow 的政策,我不得不更改示例中的域:https: //meta.stackexchange.com/questions/208963/why-are-certain-example-urls-like-http-site-com-and-http- mysite-com-blocke

回答by Demian Brecht

回答by clklachu

You can acheive this using window.open()method, passing _blankas one of the parameter. You can refer the below links which has more information on this.

您可以使用window.open()方法来实现这一点,_blank作为参数之一传递。您可以参考以下链接,其中包含有关此的更多信息。

http://www.w3schools.com/jsref/met_win_open.asp

http://www.w3schools.com/jsref/met_win_open.asp

http://msdn.microsoft.com/en-us/library/ms536651(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/ms536651(v=vs.85).aspx

Hope this will help you.

希望这会帮助你。

回答by RileyManda

If you strictly want to stick to using button,Then simply create an open window function as follows:

如果你真的想坚持使用按钮,那么只需创建一个打开的窗口功能如下:

    <script>
function myfunction() {
    window.open("mynewpage.html");
}
</script>

Then in your html do the following with your button:

然后在您的 html 中使用您的按钮执行以下操作:

Join

加入

So you would have something like this:

所以你会有这样的事情:

 <body>
    <script>
function joinfunction() {
    window.open("mynewpage.html");
}
</script>
<button  onclick="myfunction()" type="button" class="btn btn-default subs-btn">Join</button>