如何使用 jQuery 处理鼠标右键事件?

jqueryweb developmentfront end technology

要处理鼠标右键事件,请使用 jQuery 的 mousedown() 方法。鼠标左键、右键和中键也可以使用相同的方法捕获。您可以尝试运行以下代码来学习如何处理鼠标右键事件:

示例

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $('.myclass').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left mouse button is pressed');
            break;
        case 2:
            alert('Middle mouse button is pressed');
            break;
        case 3:
            alert('Right mouse button is pressed');
            break;
        default:
            alert('Nothing');
    }
});
});
</script>
</head>
<body>
<a href="http://qries.com" class="myclass">Click me</a>
</body>
</html>

相关文章