Html 如何通过按按钮在新标签中打开链接?在玉

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

How to open link in new Tab by pressing Button? in jade

javascripthtmlpug

提问by Sangram Singh

The following

下列

button(type="button", target="_blank", onclick="location.href='auth/google';")

does not work. It opens link in the same window. Just for reference, its part of node.js program, in which i'm using passportjs for google authentication.

不起作用。它在同一窗口中打开链接。仅供参考,它是 node.js 程序的一部分,我使用passportjs 进行谷歌身份验证。

回答by jcsanyi

The button isn't actually opening a link - it's just running some javascript code that happens to, in this instance, be navigating to a new URL. So the target="_blank"attribute on the button won't help.

该按钮实际上并没有打开链接——它只是运行一些 javascript 代码,在这种情况下,它会导航到一个新的 URL。所以target="_blank"按钮上的属性无济于事。

Instead, you need to use javascript commands to open a new tab/window, rather than using javascript to change the URL of the current window. Assigning to location.hrefwill only change the URL of the current window.

相反,您需要使用 javascript 命令来打开一个新的选项卡/窗口,而不是使用 javascript 来更改当前窗口的 URL。分配到location.href只会更改当前窗口的 URL。

Use the window.open(url, target)function instead - it takes a URL, and a target window name, which behaves the same as the target="whatever"attribute on a link.

改用该window.open(url, target)函数 - 它需要一个 URL 和一个目标窗口名称,其行为与target="whatever"链接上的属性相同。

window.open('auth/google', '_blank');

Your complete code would look like this:

您的完整代码如下所示:

button(type="button", onclick="window.open('auth/google', '_blank');")