因为我试图阻止 html 应用程序中所有 anchor 标记中的 ctr + click 事件。所以可以建议我如何在所有使用 javascript 或 Jquery 的浏览器中做一个有效的方法。提前致谢
<a href="myapplication1.html" >myapplication1.html</a>
<a href="myapplication2.html" >myapplication2.html</a>
<a href="myapplication3.html" >myapplication3.html</a>
最佳答案
我质疑这样做是否明智,下面的一些评论支持这一点。在打开新标签页时,FireFox 非常谨慎,我认为这是正确的。您需要一个非常好的借口来打破预期的浏览器行为。
这里所说的是一种 jQuery 方法,目前适用于 IE、Edge 和 Chrome,但不 Firefox。
//Apply to all a tags
$("a").on("mousedown", function(event){
//If middle mouse buton or normal click with ctrl
if(event.which == 2 || (event.which == 1 && event.ctrlKey)) {
console.log("Block");
//Stop default behavior
event.preventDefault();
//Let the user know what is happening and give them options
if(confirm("Please don't open links in new tabs. Click 'OK' to open the link in this window")){
//change location of current tab
window.location.href = $(this).attr("href");
}
return false;
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://www.google.com" >Google</a>
<a href="https://www.bing.com" >Bing</a>
<a href="https://stackoverflow.com" >Stack Overflow</a>
关于javascript - 如何防止或避免控制键 + 单击 anchor 标记 (href) 上的事件以在跨浏览器的新 TAB 中打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57848127/