简体   繁体   English

为什么在尝试清理事件处理程序时,该事件处理程序代码会中断?

[英]Why does this event handling code for ender break when I try to clean it up some?

This is the code that works properly: 这是正常工作的代码:

bonzo.aug({
  bind: function (event, handler) {

    if (this[0].attachEvent)
            this[0].attachEvent('on'+event, handler);
          else
            this[0].addEventListener(event, handler);
        },
  unbind: function (event, handler) {
            if (this[0].detachEvent)
              this[0].detachEvent('on'+event, handler);
            else
              this[0].removeEventListener(event, handler);
          },
  once: function (event, handler) {
          function doOnce(e) {
            bonzo(this).unbind(event, doOnce);
            handler.call(this, e);
          }
          this.bind(event, doOnce);
        }
});

But then when I try to consolidate and soup parts of it up a little, unbind and once break: 但后来当我试图巩固和它的一个小零件汤, unbindonce突破:

(function($){
  $.ieEventApi = !!window.attachEvent; // !-[1,];

  $.addEventListener = $.ieEventApi ? "attachEvent" : "addEventListener",
  $.removeEventListener = $.ieEventApi ? "detachEvent" : "removeEventListener",
  $.onForIe = $.ieEventApi ? 'on' : '',
  $.adaptEventHandlerForIe = function(f){
    return function(e){
      e.target = e[(e.target ? e.target : (e.srcElement || document))];
      return f(e);
    };
  };



  $.aug({
    bind: function (event, handler) {
            for(var i = 0; i < this.length; i++) // I'd use Bonzo.each if I could find any documentation for its use.. :-\
                this[i][$.addEventListener]($.onForIe+event, $.adaptEventHandlerForIe(handler), false); // The "false" is superfluous on IE, but apparently not problematically so.
            return this;
          },
    unbind: function (event, handler) {
              for(var i = 0; i < this.length; i++)
                this[i][$.removeEventListener]($.onForIe+event, $.adaptEventHandlerForIe(handler), false); 
              return this;
            },
    once: function (event, oncehandler) {
            // This just calls the other two, which already handle iteration.
            this.bind(event, doOnce);
            return this;

            function doOnce(e) {
              $(e.target /*Or should I be using e.target here?*/ ).unbind(event, doOnce);
              oncehandler.call(this, e);
            }
          }
  });
})(bonzo);

Shouldn't the following line 不应该以下行

e.target = e[(e.target ? e.target : (e.srcElement || document))];

be something like

e.target = e[e.target ? 'target' : (e.srcElement ? 'srcElement' : 'document')];

? I mean, when you're referring to a prop in an object and using square brackets you should write the inner value in string format. 我的意思是,当您在对象中引用道具并使用方括号时,应以字符串格式编写内部值。 So e.etc should become e['etc'] . 因此e.etc应该成为e['etc']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将方法附加到类原型时,为什么代码会中断? - Why does my code break when I try to attach my methods to the class prototype? 尝试刷新usestate时为什么程序会中断? - Why does my program break when I try to refresh the usestate? 为什么当我删除“.longest”时这段代码会中断? [等候接听] - Why does this code break when I remove the “.longest”? [on hold] 为什么这段代码中的js事件没有冒泡? - Why does the js event in this code not bubble up? 是什么导致此事件处理程序继续运行?为什么当我尝试添加条件时它会停止? - What causes this event handler to keep running?, and why does it halt when I try to add a condition? 为什么当我尝试将事件变量作为参数提供给 function 时,JS 会划掉事件变量? - Why does JS cross out the event variable when I try to give it to an function as an argument? 为什么这个焦点事件在按 Tab 时会破坏 html 样式? - Why does this focus event break html style when pressing tab? 当我将 Tableau 示例中的 URL 更改为另一个 URL 时,为什么我的代码会中断? - Why does my code break when I change the URL in the Tableau sample to another URL? 当我将脚本类型设置为模块时,为什么我的 D3 代码会中断? - Why does my D3 code break when I set the script type to module? 为什么 jQuery 会破坏这段代码? - Why does jQuery break this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM