如何使用 jQuery 處理滑鼠右鍵單擊事件?


要處理滑鼠右鍵單擊事件,請使用 mousedown() jQuery 方法。還可以使用相同的方法捕獲滑鼠左鍵、右鍵和中間鍵的單擊。您可以嘗試執行以下程式碼,以瞭解如何處理滑鼠右鍵單擊事件

示例

即時 Demo

<!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>

更新時間: 2020 年 2 月 14 日

3K+ 瀏覽

開啟您的職業

完成課程認證

開始
廣告
© . All rights reserved.