如何使用 jQuery 多事件觸發同一個功能?


要使用多個事件觸發相同的功能,請在 on() 方法中使用多個 jQuery 事件,例如單擊、雙擊、滑鼠進入、滑鼠離開、懸停等。

示例

可以嘗試執行以下程式碼,瞭解如何使用 jQuery 多個事件處理同一個功能

實際演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").on({
   
        mouseenter: function(){
            $(this).css("background-color", "gray");
        },  
       
        mouseleave: function(){
            $(this).css("background-color", "red");
        },
       
        dblclick: function(){
            $(this).css("background-color", "yellow");
        }
       
    });
});
</script>
</head>
<body>

<p>Click, double click and move the mouse pointer.</p>

</body>
</html>

更新日期: 2019 年 12 月 11 日

4K+ 瀏覽量

開啟你的 職業生涯

完成課程,獲取認證

開始學習
廣告
© . All rights reserved.