简体   繁体   English

停止在单击鼠标中键时打开新选项卡

[英]stop opening a new tab on click mouse middle button

I have on anchor tag in htm. 我在htm中有锚标签。 when I click mouse middle button on anchor tag then new tab is opened. 当我单击锚标记上的鼠标中键时,将打开新选项卡。 But I want to stop it's working. 但我想停止它的运作。 I tried lot of tricks in javascript but it is not working. 我在javascript中尝试了很多技巧,但无法正常工作。 Can anybody have solution for that? 有人可以为此解决吗?

<a class="tag" href="www.google.com>google</a>

I don't want to open a new tab by clicking with mouse middle button over anchor tag having class (tag)... prevent mouse middle button to open new tab on anchor tab with a particular id. 我不想通过在具有类(标记)的锚标记上单击鼠标中键来打开新选项卡...防止鼠标中键在具有特定ID的锚选项卡上打开新选项卡。

I found solution as per my requirement 我根据我的要求找到了解决方案

<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script>
$(function(){
  $(document).on("click", function(e){
    if($(e.target).is("#google") && e.button===1)
      e.preventDefault()
  })
})
</script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<a href="http://google.com" id="google">Google</a><br> <a href="http://bing.com" id="bing">Bing</a>
</body>
</html>

fiddle link 小提琴链接

I agree with @Matt Lishman in the comments: don't. 我同意@Matt Lishman的评论:不。

But to give you a solution: 但是给你一个解决方案:

Your code is almost right. 您的代码几乎是正确的。 Only (as @GoTo says), mouseup is to late. 只是(如@GoTo所说), mouseup太晚了。 When you listen for click events you can check the which property on the event object. 当你听的click事件,你可以检查which事件对象的属性。 The which is 2 when you click with the scrollwheel. which2 ,当您点击与滚轮。

So, if which === 2 , preventDefault 因此,如果which === 2 ,则preventDefault

https://jsfiddle.net/k3o5pt6c/ https://jsfiddle.net/k3o5pt6c/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM