我有一个简单的问题:我有两个不同的函数绑定到click事件:
$("#selector").click(function one() {
// stuff
});
$("#selector").click(function two() {
// other stuff
});我只想解绑其中的一个。我该怎么做?
发布于 2020-05-07 05:24:13
您需要将绑定的函数保存到变量中。之后,您可以使用jQuery的.off()-method解除绑定:
(function($){
  var alert1 = function(){
    alert('1');
  }, alert2 = function(){
    alert('2');
  };
  
  // Bind alert1
  $('button').on('click',alert1);
  
  // Bind alert 2
  $('button').on('click',alert2);
  
  // Unbind alert2
  $('button').off('click',alert2);
})(jQuery);<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Alert</button>
发布于 2020-05-07 05:27:12
off function就是您要查找的内容
The .off() method removes event handlers that were attached with .on()
https://stackoverflow.com/questions/61645566
复制相似问题