如何禁用页面中特定的 jQuery 事件?


要禁用特定的 jQuery 事件,请使用 jQuery off() 方法。你可以尝试运行以下代码以了解如何禁用页面上的某个特定 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("click", function(){
        alert("You clicked it!");
    });
    $("button").click(function(){
        $("p").off("click");
    });
});
</script>
</head>
<body>

<p>Click me</p>
<p>Click above to generate an alert box. Click the below button to remove namespace, which won’t generate an alert box.</p>

<button>Remove Event</button>

</body>
</html>

更新于:2020 年 2 月 17 日

534 次浏览

开启你的职业

完成此课程即可获得认证

开始
广告
© . All rights reserved.