HTML 按钮在新标签页中打开链接

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

HTML button opening link in new tab

htmlweb

提问by RDR

So this is the simple code for the button to open a certain link

所以这是按钮打开某个链接的简单代码

                <button class="btn btn-success" onclick="location.href='http://google.com';"> Google</button>

but it opens it on the same page, I want the link to open on a new tab though.

但它在同一页面上打开它,但我希望链接在新选项卡上打开。

回答by ngLover

You can use the following.

您可以使用以下内容。

window.open(
  'https:http://google.com',
  '_blank' // <- This is what makes it open in a new window.
);

in HTML

在 HTML 中

 <button class="btn btn-success" onclick=" window.open('http://google.com','_blank')"> Google</button>

plunkr

普朗克

回答by EstevaoLuis

With Bootstrap you can use an anchor like a button.

使用 Bootstrap,您可以像按钮一样使用锚点。

<a class="btn btn-success" href="https://www.google.com" target="_blank">Google</a>

And use target="_blank"to open the link in a new tab.

并用于target="_blank"在新选项卡中打开链接。

回答by Pavel Chernikov

You can also add this to your form:

您还可以将其添加到表单中:

<form ... target="_blank">

回答by Ashish Raj

Try using below code:

尝试使用以下代码:

<button title="button title" class="action primary tocart" onclick=" window.open('http://www.google.com', '_blank'); return false;">Google</button>

Here, the window.openwith _blankas second argumentof window.openfunction will open the link in new tab.

在这里,window.open_blank作为第二个参数window.open功能将打开新的标签页的链接。

And by the use of return falsewe can remove/cancel the default behavior of the button like submit.

通过使用return false我们可以删除/取消按钮的默认行为,如提交。

For more detail and live example, click here

有关更多详细信息和现场示例,请单击此处

回答by Krishnaraj

Try this

尝试这个

window.open(urlValue, "_system", "location=yes");

回答by Harshwardhan S

Use '_blank'. It will not only open the link in a new tab but the state of the original webpage will also remain unaffected.

使用'_blank'。它不仅会在新选项卡中打开链接,而且原始网页的状态也不会受到影响。

回答by rushabhpathak

Try this code.

试试这个代码。

<input type="button" value="Open Window"
onclick="window.open('http://www.google.com')">

回答by Yanay Lehavi

If you're in pug:

如果你在哈巴狗:

html
    head
        title Pug
    body
        a(href="http://www.example.com" target="_blank") Example
        button(onclick="window.open('http://www.example.com')") Example

And if you're puggin' Semantic UI:

如果你正在研究语义 UI:

html
    head
        title Pug
        link(rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css')
    body
        .ui.center.aligned.container
            a(href="http://www.example.com" target="_blank") Example
        .ui.center.aligned.container
           .ui.large.grey.button(onclick="window.open('http://www.example.com')") Example