如何暫時抑制 jQuery 事件處理?


要暫時抑制 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(){
    $(element).click(function(e) {
        e.preventDefault();

        // Check for fired class
        if ($(this).hasClass('fired') == false) {
          // Add fired class
          $(element).addClass('fired');

          // Remove fired class
          $(element).removeClass('fired');  
        }
   });
});
</script>
<style>
.fired {
    font-size: 25px;
    color: green;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p class="fired">This is a paragraph.</p>
<p class="fired">This is second paragraph.</p>
</body>
</html>

更新日期: 2020 年 2 月 14 日

587 次瀏覽

啟動您的 事業

完成課程以獲得認證

開始
廣告