📜  如何使用jQuery区分鼠标左键和右键单击?

📅  最后修改于: 2021-11-24 04:34:54             🧑  作者: Mango

jQuery 可用于区分正在单击的鼠标按钮。这可用于各种 Web 应用程序,其中知道某个鼠标按钮可用于触发某些功能。

使用 mousedown() 方法:

jQuery 中的mousedown()方法可用于将事件处理程序绑定到默认的‘mousedown’ JavaScript 事件。这可用于触发事件。然后可以使用事件对象的“which”属性来检查相应的鼠标按钮。

句法:

$( "#target" ).mousedown(function() {
  alert( "Function to be handles here" );
});

“哪个”属性:
根据按下的鼠标按钮,which 属性有 3 个值。我们可以使用switch case来获取对应的值。

  • 1 为鼠标左键
  • 2 为鼠标中键
  • 3 为鼠标右键

例子:



  

    
      How to distinguish left and right 
      mouse click using jQuery?
  

  

    

      GeeksforGeeks   

           How to distinguish left and        right mouse click using jQuery?        

You are pressing the :         

  
                         

输出:
用鼠标左键单击:
左键

用鼠标中键单击:
中键

用鼠标右键单击:
右键