<bdo id='zg4t6'></bdo><ul id='zg4t6'></ul>
  • <small id='zg4t6'></small><noframes id='zg4t6'>

    1. <legend id='zg4t6'><style id='zg4t6'><dir id='zg4t6'><q id='zg4t6'></q></dir></style></legend>

        <i id='zg4t6'><tr id='zg4t6'><dt id='zg4t6'><q id='zg4t6'><span id='zg4t6'><b id='zg4t6'><form id='zg4t6'><ins id='zg4t6'></ins><ul id='zg4t6'></ul><sub id='zg4t6'></sub></form><legend id='zg4t6'></legend><bdo id='zg4t6'><pre id='zg4t6'><center id='zg4t6'></center></pre></bdo></b><th id='zg4t6'></th></span></q></dt></tr></i><div id='zg4t6'><tfoot id='zg4t6'></tfoot><dl id='zg4t6'><fieldset id='zg4t6'></fieldset></dl></div>

        <tfoot id='zg4t6'></tfoot>

        如何删除带有匿名函数的 addEventListener 的EventLi

        时间:2023-09-05
        • <bdo id='UOFby'></bdo><ul id='UOFby'></ul>
            <tbody id='UOFby'></tbody>
        • <tfoot id='UOFby'></tfoot>

            1. <small id='UOFby'></small><noframes id='UOFby'>

              <legend id='UOFby'><style id='UOFby'><dir id='UOFby'><q id='UOFby'></q></dir></style></legend>
            2. <i id='UOFby'><tr id='UOFby'><dt id='UOFby'><q id='UOFby'><span id='UOFby'><b id='UOFby'><form id='UOFby'><ins id='UOFby'></ins><ul id='UOFby'></ul><sub id='UOFby'></sub></form><legend id='UOFby'></legend><bdo id='UOFby'><pre id='UOFby'><center id='UOFby'></center></pre></bdo></b><th id='UOFby'></th></span></q></dt></tr></i><div id='UOFby'><tfoot id='UOFby'></tfoot><dl id='UOFby'><fieldset id='UOFby'></fieldset></dl></div>
                  本文介绍了如何删除带有匿名函数的 addEventListener 的EventListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  function doSomethingWith(param)
                  {
                      document.body.addEventListener(
                          'scroll',
                          function()
                          {
                              document.write(param);
                          },
                          false
                      ); // An event that I want to remove later
                  }
                  setTimeout(
                      function()
                      {
                          document.body.removeEventListener('scroll', HANDLER ,false);
                              // What HANDLER should I specify to remove the anonymous handler above?
                      },
                      3000
                  );
                  doSomethingWith('Test. ');
                  

                  推荐答案

                  你不能.您必须使用命名函数或以某种方式存储引用.

                  You can't. You have to use a named function or store the reference somehow.

                  var handler;
                  
                  function doSomethingWith(param) {
                      handler = function(){
                          document.write(param);
                      };  
                      document.body.addEventListener('scroll', handler,false);
                  }
                  setTimeout(function() {
                       document.body.removeEventListener('scroll', handler ,false);
                  }, 3000);
                  

                  最好以结构化的方式执行此操作,以便您可以识别不同的处理程序并将其删除.在上面的示例中,您显然只能删除最后一个处理程序.

                  The best would be to do this in a structured way, so that you can identify different handlers and remove them. In the example above, you obviously could only remove the last handler.

                  更新:

                  您可以创建自己的处理程序处理程序 (:)):

                  You could create your own handler handler (:)) :

                  var Handler = (function(){
                      var i = 1,
                          listeners = {};
                  
                      return {
                          addListener: function(element, event, handler, capture) {
                              element.addEventListener(event, handler, capture);
                              listeners[i] = {element: element, 
                                               event: event, 
                                               handler: handler, 
                                               capture: capture};
                              return i++;
                          },
                          removeListener: function(id) {
                              if(id in listeners) {
                                  var h = listeners[id];
                                  h.element.removeEventListener(h.event, h.handler, h.capture);
                                  delete listeners[id];
                              }
                          }
                      };
                  }());
                  

                  然后你可以使用它:

                  function doSomethingWith(param) {
                      return Handler.addListener(document.body, 'scroll', function() {
                          document.write(param);
                      }, false);
                  }
                  
                  var handler = doSomethingWith('Test. ');
                  
                  setTimeout(function() {
                       Handler.removeListener(handler);
                  }, 3000);
                  

                  演示

                  这篇关于如何删除带有匿名函数的 addEventListener 的EventListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:DOM 事件优先级 下一篇:替代支持 eventData 的 jQuery 的 .toggle() 方法?

                  相关文章

                  最新文章

                    <bdo id='tVEpP'></bdo><ul id='tVEpP'></ul>

                  <small id='tVEpP'></small><noframes id='tVEpP'>

                • <i id='tVEpP'><tr id='tVEpP'><dt id='tVEpP'><q id='tVEpP'><span id='tVEpP'><b id='tVEpP'><form id='tVEpP'><ins id='tVEpP'></ins><ul id='tVEpP'></ul><sub id='tVEpP'></sub></form><legend id='tVEpP'></legend><bdo id='tVEpP'><pre id='tVEpP'><center id='tVEpP'></center></pre></bdo></b><th id='tVEpP'></th></span></q></dt></tr></i><div id='tVEpP'><tfoot id='tVEpP'></tfoot><dl id='tVEpP'><fieldset id='tVEpP'></fieldset></dl></div>
                  <tfoot id='tVEpP'></tfoot>

                  <legend id='tVEpP'><style id='tVEpP'><dir id='tVEpP'><q id='tVEpP'></q></dir></style></legend>