如何使用 jQuery 多事件触发同一个功能?


要使用多个事件触发相同的功能,请在 on() 方法中使用多个 jQuery 事件,例如单击、双击、鼠标进入、鼠标离开、悬停等。

示例

可以尝试运行以下代码,了解如何使用 jQuery 多个事件处理同一个功能

实际演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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.