Skip to main content
 首页 » 编程设计

jquery之使用另一个按钮反转由 .off() 方法引起的事件处理程序的删除

2026年04月16日950txw1958

我在谷歌上搜索了几个小时试图弄清楚这一点。

基本上我的情况是我必须找到一种方法来逆转.off() 方法引起的事件处理程序的删除。

换句话说,我希望能够使用另一个按钮重新打开 p 标记的事件处理程序。我怎样才能做到这一点?这是我的代码示例:

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $("p").on("click", function(){ 
        $(this).css("background-color", "pink"); 
    }); 
    $("#turn-off").click(function(){ 
        $("p").off("click"); 
        $('p').css("background-color", "white"); 
    }); 
     
    $("#reverse").click(function(){ 
       //??? 
    }); 
}); 
</script> 
</head> 
<body> 
 
<p>Click this paragraph to change its background color.</p> 
<p>Click the button below and then click on this paragraph (the click event is removed).</p> 
 
<button id='turn-off'>Remove the click event handler</button> 
 
<button id='reverse'>Reverse the removal of the click event handler</button> 
 
</body> 
</html>

请您参考如下方法:

重新打开它只是再次调用.on。而且,由于您再次调用 .on,因此您需要提供所有参数 - 就像第一次一样。

为了更简单,您可以将调用 .on 的代码移至一个函数,并在 #reverse 点击逻辑中使用它。

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    function turnOn() { 
      $("p").on("click", function(){ 
        $(this).css("background-color", "pink"); 
      }); 
    } 
    turnOn(); // initial setup 
    $("#turn-off").click(function(){ 
        $("p").off("click"); 
        $('p').css("background-color", "white"); 
    }); 
     
    $("#reverse").click(function(){  // or, simply: $("#reverse").click(turnOn); 
       turnOn(); 
    }); 
}); 
</script> 
</head> 
<body> 
 
<p>Click this paragraph to change its background color.</p> 
<p>Click the button below and then click on this paragraph (the click event is removed).</p> 
 
<button id='turn-off'>Remove the click event handler</button> 
 
<button id='reverse'>Reverse the removal of the click event handler</button> 
 
</body> 
</html>


另外,我建议您使用namespaced events ,将来应该会为您省去一些麻烦(注意使用 click.foo 而不是 click,因为 foo 是命名空间):

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    function turnOn() { 
      $("p").on("click.foo", function(){ 
        $(this).css("background-color", "pink"); 
      }); 
    } 
    turnOn(); // initial setup 
    $("#turn-off").click(function(){ 
        $("p").off("click.foo"); 
        $('p').css("background-color", "white"); 
    }); 
     
    $("#reverse").click(turnOn); 
}); 
</script> 
</head> 
<body> 
 
<p>Click this paragraph to change its background color.</p> 
<p>Click the button below and then click on this paragraph (the click event is removed).</p> 
 
<button id='turn-off'>Remove the click event handler</button> 
 
<button id='reverse'>Reverse the removal of the click event handler</button> 
 
</body> 
</html>