使用jQuery禁用鼠标滚动中点击事件 [英] Disable mouse scroll middle click event with jQuery

查看:72
本文介绍了使用jQuery禁用鼠标滚动中点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码,以在单击鼠标时禁用滚动. 但是我的代码不起作用,当我用鼠标滚动单击时,它会打开我的链接.

I wrote the below code for disabling the scroll in the mouse while it is clicked. but my code does not work an when I click with the scroll of my mouse it opens my link.

这是我的代码:

$('a').on('mousedown', function(e) {
  if (e.which === 2) {
    console.log('Disabled');
    return false;
  }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<a href="#dontwanttoopen"> click here </a>

推荐答案

您必须使用auxclick才能禁用此功能.将"click"替换为"auxclick",然后添加e.preventDefault(),它将在chrome和FF中进行测试

You have to use auxclick in order to disable this feature. Replace your 'click' with 'auxclick', and add e.preventDefault(), it will work, tested in chrome and FF

$('a').on('auxclick', function(e) {
  if (e.which === 2) {
    e.preventDefault();
  }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<a href="#dontwanttoopen"> click here </a>

这篇关于使用jQuery禁用鼠标滚动中点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆