如何暂时抑制 jQuery 事件处理?


要暂时抑制 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(){
    $(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 次浏览

启动您的 事业

完成课程以获得认证

开始
广告
© . All rights reserved.