jQuery 当链接在新标签页中打开时如何留在当前窗口?

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

How to stay on current window when the link opens in new tab?

jqueryhtml

提问by EnexoOnoma

When a user clicks on a link

当用户点击链接时

<a href="http://www.stackoverflow.com" target="_blank">click</a>

is there a way to stay on the current window instead of going to the tab ?

有没有办法留在当前窗口而不是转到选项卡?

采纳答案by alex

Is there a way to stay on the current window instead of going to the tab [when the link has target="_blank"] ?

有没有办法留在当前窗口而不是转到选项卡[当链接有时target="_blank"]?

Only if you do something like this first...

只有当你先做这样的事情......

$('a[target="_blank"]').removeAttr('target');

回答by Jamie

<a href="www.stackoverflow.com" onclick="window.open('#','_blank');window.open(this.href,'_self');">

<a href="www.stackoverflow.com" onclick="window.open('#','_blank');window.open(this.href,'_self');">

This will load the current web page in a new tab which the browser will focus on, and then load the href in the current tab

这将在浏览器将关注的新选项卡中加载当前网页,然后在当前选项卡中加载 href

回答by Adel Bachene

I guess target="_blank" would open new tab/Windows but will switch the tab as well, and no way I can find them stuff in html, Yes but when we click in link pressing control key it opens the link in new background tab, Using javascript we can stimulate same Here is code I found

我猜 target="_blank" 会打开新选项卡/Windows,但也会切换选项卡,我无法在 html 中找到它们,是的,但是当我们点击链接时按下控制键,它会在新的背景选项卡中打开链接, 使用 javascript 我们可以刺激相同的这是我找到的代码

function openNewBackgroundTab(){    
    var a = document.createElement("a");    
    a.href = "http://www.google.com/";    
    var evt = document.createEvent("MouseEvents");    

    //the tenth parameter of initMouseEvent sets ctrl key    
    evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,true, false, false, false, 0, null);    
    a.dispatchEvent(evt);
}

回答by Martin Jespersen

It can be done easily using javascript to intercept all clicks through a delegate function and then calling preventDefault()on the event. After that it is a matter of creating a pop-under window just like a nasty ad ;)

可以很容易地使用 javascript 通过委托函数拦截所有点击,然后调用preventDefault()事件。之后就是像讨厌的广告一样创建一个弹出窗口的问题;)

That said, don't do this unless you plan on pissing your users off :P

也就是说,除非您打算激怒您的用户,否则不要这样做:P

回答by Anita Whiteson

Try this (I found it useful for playing audio files in the backgroundwithout distracting the user from the current page or using script.)

试试这个(我发现它对于在后台播放音频文件很有用,而不会分散用户对当前页面或使用脚本的注意力。)

<a href="first.mp3" target="yourframename"> First Song </a>
<a href="second.mp3" target="yourframename"> Second Song </a>

The first time a user clicks on the link, the target window will be on top. Any subsequent clicks leave the current window on top. Essentially, the links open in the backgroundwindow because there is no <frame> or <iframe> specified.

用户第一次单击链接时,目标窗口将位于顶部。任何后续点击都会将当前窗口留在顶部。本质上,链接在后台窗口中打开,因为没有指定 <frame> 或 <iframe>。

Only works on Opera, Mozilla and IE (the versions on my computer). Doesn't work for Chrome and Safari.

仅适用于 Opera、Mozilla 和 IE(我电脑上的版本)。不适用于 Chrome 和 Safari。

回答by Rusty Fausak

No, this is controlled by the browser.

不,这是由浏览器控制的。