如何在頁面上停用特定的 jQuery 事件?


要停用特定的 jQuery 事件,請使用 jQuery off() 方法。你可以嘗試執行以下程式碼來了解如何停用頁面上的特定 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("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.