<i id='8d6Iz'><tr id='8d6Iz'><dt id='8d6Iz'><q id='8d6Iz'><span id='8d6Iz'><b id='8d6Iz'><form id='8d6Iz'><ins id='8d6Iz'></ins><ul id='8d6Iz'></ul><sub id='8d6Iz'></sub></form><legend id='8d6Iz'></legend><bdo id='8d6Iz'><pre id='8d6Iz'><center id='8d6Iz'></center></pre></bdo></b><th id='8d6Iz'></th></span></q></dt></tr></i><div id='8d6Iz'><tfoot id='8d6Iz'></tfoot><dl id='8d6Iz'><fieldset id='8d6Iz'></fieldset></dl></div>
    • <bdo id='8d6Iz'></bdo><ul id='8d6Iz'></ul>
    1. <legend id='8d6Iz'><style id='8d6Iz'><dir id='8d6Iz'><q id='8d6Iz'></q></dir></style></legend>

    2. <small id='8d6Iz'></small><noframes id='8d6Iz'>

      <tfoot id='8d6Iz'></tfoot>

        如何模仿 stopImmediatePropagation() 的行为(不使用 j

        时间:2023-09-04

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

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

                <bdo id='PIVmj'></bdo><ul id='PIVmj'></ul>
                  本文介绍了如何模仿 stopImmediatePropagation() 的行为(不使用 jquery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在为一个 Javascript 库编写事件处理代码,并且我正在尝试实现类似于 stopImmediatePropagation() 的东西,它也适用于 IE 6.

                  I'm working on the event-handling code for a Javascript library, and I'm trying to implement something similar to stopImmediatePropagation() that will also work in IE 6.

                  目前事件处理的工作方式是,我们的事件处理代码向对象注册,然后用户向我们的事件处理程序注册他们所有的事件.

                  The way the event handing works currently, is that our event handing code registers with the object, and then users register all their events with our event handlers.

                  我尝试模拟 stopImmediatePropagation() 的第一种方法是简单地将该方法添加到事件中(如果它尚不存在):

                  The first way I tried to emulate stopImmediatePropagation() was to simply add that method to the event if it didn't already exist:

                  if (event != null && event.isImmediatePropagationEnabled == null) {
                      event.stopImmediatePropagation = function () {
                          this.isImmediatePropagationEnabled = false;
                      };
                      event.isImmediatePropagationEnabled = true;
                      event.isImmediatePropagationStopped = function () {
                          return !this.isImmediatePropagationEnabled;
                      };
                  }
                  

                  当用户的事件处理回调被调用时,它们被传递到我们得到的同一个事件对象中.如果他们愿意,他们可以在事件上调用 stopImmediatePropagation().

                  When the users' event handler callbacks are called, they're passed in the same event object we get. They can call stopImmediatePropagation() on the event if they wish.

                  当我们遍历所有向我们注册的事件处理回调时,我们每次都检查传播布尔值:

                  As we're looping through all the event handling callbacks registered with us, we check the propagation boolean each time:

                  given some event
                  for [all the callbacks] {
                      if (event != null &&
                          event.isImmediatePropagationStopped != null &&
                          event.isImmediatePropagationStopped()) {
                          stopPropagation = true;
                          break;
                      }
                  
                      execute the callback, passing in the event
                  }
                  

                  这在某些浏览器中效果很好.因为事件持续存在,即使我们的事件处理代码退出并且事件冒泡到下一个元素,一旦我们的事件处理代码再次被命中,isImmediatePropagationStopped 属性仍然存在,因此不再执行回调(向我们注册的).

                  This works great in some browsers. Because the event persists, even once our event handling code exits and the event bubbles up to the next element, once our event handing code gets hit again the isImmediatePropagationStopped property still exists, and so no more callbacks (that are registered with us) are executed.

                  在 Internet Explorer(甚至 8)中,这不起作用.在同一个元素上,一切都很好;但是一旦事件冒泡,似乎生成了一个全新的事件对象,并且我们失去了 isImmediatePropagationStopped 属性.这意味着我们无法检查用户是否关闭了传播.

                  In Internet Explorer (even in 8), this doesn't work. On the same element, things are fine; but once the event bubbles, it seems like an entirely new event object is generated, and we lose the isImmediatePropagationStopped property. This means we can't check if the user turned off propagation.

                  所以我的问题是,有没有人对如何做到这一点有任何想法?我知道 jquery 管理类似的壮举( http://bugs.jquery.com/ticket/3355 ),但它们以类似的方式进行 - 将其存储在对象的额外数据中.我不知道的是,物体的不持久性如何不会像我一样伤害他们.(而且由于各种原因,这里不能使用 jquery 本身)

                  So my question is, does anyone have any ideas on how to do this? I know that jquery manages a similar feat ( http://bugs.jquery.com/ticket/3355 ), but they do it a similar way - storing it in extra data for the object. What I don't know is how the non-persistence of the object doesn't hurt them like it does me. (And for various reasons, using jquery itself is not an option here)

                  如果有人有任何见解——要么是因为比我更了解 Javascript,要么是因为他们能想到一种巧妙的方法来做到这一点——我将不胜感激.谢谢!

                  If anyone has any insights - either due to knowing more about Javascript than me, or because they can think of a neat way to do this - I'd greatly appreciate it. Thanks!

                  推荐答案

                  试着改变你的函数:

                  event.stopImmediatePropagation = function () {
                      this.isImmediatePropagationEnabled = false;
                      this.cancelBubble = true;
                  };
                  

                  cancelBubble"标志是 IE 的东西,它应该防止事件冒泡父 DOM 元素链.

                  That "cancelBubble" flag is an IE thing, and it should prevent the event from bubbling up the parent DOM element chain.

                  这篇关于如何模仿 stopImmediatePropagation() 的行为(不使用 jquery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:确保 jQuery 事件处理程序执行顺序 下一篇:如何在 jQuery 中制作实时自定义事件

                  相关文章

                  最新文章

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

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

                    • <bdo id='TWH7O'></bdo><ul id='TWH7O'></ul>

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