![](/img/trans.png)
[英]Prevent Firefox from opening svg images in a new tab on middle mouse click
[英]Prevent Middle Click button from opening new tab in FIREFOX not working
我有以下簡單的代碼 - 我有一個更復雜的東西,但這說明了我的觀點 - 。
我有以下 html
<body>
<a id="somelink" href="http://www.google.com"
onmousedown="preventOpen(event,this)" onclick="return
confirmClick();">Click me</a>
</body>
以及以下 javascript 代碼
<script>
function preventOpen(event, element){
event = event || window.event;
event.preventDefault();
if (event.which != 3) {
element.click();
}
}
function confirmClick(){
return confirm("Are you sure?");
}
</script>
基本上,我試圖在單擊鏈接時提示確認是否使用鼠標中鍵滾輪打開新選項卡-我想阻止打開新選項卡-。 這在 Chrome 中很有效,但是,它在 Firefox 中不起作用-退出 javascript 代碼的那一刻-即“confirmClick”函數-,它打開一個新選項卡-,盡管它成功觸發了 preventDefault()。 關於為什么這在 FF 中不起作用的任何想法? 我現在寧願遠離 jQuery 回復。
我想正確的方法是使用auxclick
事件。 ( MDN 上的 auxclick )
window.addEventListener("auxclick", (event) => {
if (event.button === 1) {
if (!confirm("Are you sure?")) event.preventDefault();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.