当用户点击链接时
<a href="http://www.stackoverflow.com" target="_blank">click</a>
有没有一种方法可以停留在当前窗口,而不是跳转到选项卡?当用户点击链接时
<a href="http://www.stackoverflow.com" target="_blank">click</a>
有没有一种方法可以停留在当前窗口,而不是跳转到选项卡?我猜target="_blank"会在新的标签页或窗口打开链接,但是也会切换到该标签页,而且我找不到任何与此相关的html标签。
但是当我们按住control键点击链接时,它会在新的后台标签页中打开。
使用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);
}
<a href="www.stackoverflow.com" onclick="window.open('#','_blank');window.open(this.href,'_self');">
这将在一个新的标签页中加载当前网页,并使浏览器聚焦于该标签页,然后在当前标签页中加载 href。
window.open(window.location.href,'_blank')
来解决),但这可能会很麻烦。 - Alvaro Montoro<a href="first.mp3" target="yourframename"> First Song </a>
<a href="second.mp3" target="yourframename"> Second Song </a>
不,这由浏览器控制。
window.open(window.location.href)
window.location.href = new_url
可以使用JavaScript轻松地通过委托函数拦截所有点击事件,然后在事件上调用preventDefault()
来实现。之后就是创建一个类似于讨厌的广告一样的弹出窗口的问题了 ;)
话虽如此,除非你打算惹恼你的用户,否则不要这样做 :P
它会在新标签页或窗口中打开 target="_blank"
保持在当前标签页或窗口中 target="_self"
目前还没有控制这个的方法。
如果您想删除现有的行为:
是否有一种方法可以停留在当前窗口而不是转到标签页[当链接具有
target="_blank"
]?
只有在您首先执行以下操作时才能实现...
$('a[target="_blank"]').removeAttr('target');
target
为 _blank
)- 这在这里得到了正确的回答。 - Levite