【问题标题】:Unbind all event handlers, do something and then bind again with jquery取消绑定所有事件处理程序,做一些事情,然后用 jquery 再次绑定
【发布时间】:2014-05-03 00:25:51
【问题描述】:

我试图存储一个元素的所有事件处理程序,然后取消绑定它们,然后做一些事情然后再次绑定,但是当我取消绑定处理程序时,带有处理程序的变量被修改为一个空对象

  var events = $._data(this.$element.get(0), 'events');
  console.log(events);    //here the variable events contains an object with the handlers
  this.$element.unbind();
  console.log(events);   //here the variable events contains an empty object

  //function to bind again the handlers
  $.each(events, function() {
    // iterate registered handler of original
    $.each(this, function() {
      $('#target').bind(this.type, this.handler);
    });
  });

【问题讨论】:

标签: jquery bind unbind


【解决方案1】:

您可以在深度模式下使用extend复制events中的数据:

var events = $._data(this.$element.get(0), 'events'),
    eventsCopy = $.extend(true, {}, events);
this.$element.unbind();
$.each(eventsCopy, function() {
    $.each(this, function() {
        $('#target').bind(this.type, this.handler);
    });
});

【讨论】:

    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多