gpt4 book ai didi

javascript - 无法使用 jquery 的单击事件处理程序来检测右键单击

转载 作者:数据小太阳 更新时间:2023-10-29 04:56:09 27 4
gpt4 key购买 nike

在尝试使用 jquery 检测鼠标右键单击时,我注意到单击鼠标右键似乎不会触发 click 事件处理程序,而 mousedown 或 mouseup 事件处理程序会触发。

例如,右键单击测试 div 后,以下警报“正在测试!”:

$('#test').mousedown(function(e) {
alert('testing');
});

但是,以下不会:

$('#test').click(function(e) {
alert('testing!');
});

有人知道为什么吗?

最佳答案

当你按下 mousedown 时,even fired 有 event.which

取自此处:How to distinguish between left and right mouse click with jQuery

$('#element').mousedown(function(event) {
switch (event.which) {
case 1:
alert('Left mouse button pressed');
break;
case 2:
alert('Middle mouse button pressed');
break;
case 3:
alert('Right mouse button pressed');
break;
default:
alert('You have a strange mouse');
}
});

因此不要使用 .click(),而是使用 mousedown 并检查大小写。

关于javascript - 无法使用 jquery 的单击事件处理程序来检测右键单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7343117/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com