加载中...

event.stopImmediatePropagation()


概述

返回值:Object

描述: 阻止剩余的事件处理函数执行并且防止事件冒泡到DOM树上。

  • V : 1.3event.stopImmediatePropagation()

    • 这个方法不接受任何参数。

除了阻止元素上其它的事件处理函数的执行,这个方法还会通过在内部调用 event.stopPropagation() 来停止事件冒泡。如果仅仅想要停止事件冒泡到祖辈元素上,而让这个元素上的其它事件处理函数继续执行,我们可以使用 event.stopPropagation() 来代替。

使用 event.isImmediatePropagationStopped() 来确定这个方法是否(在那个事件对象上)调用过了。

Additional Notes:(其他注意事项:)

    自从.live()方法处理事件一旦传播到文档的顶部,live事件是不可能停止传播的。同样地,.delegate() 事件将始终传播给其中包含的被委托元素; 元素上的事件将在被委托事件被调用的时候执行。因此,这些处理程序,可以调用event.stopPropagation()或者返回false防止委派处理程序冒泡。

示例

阻止调用其它事件处理函数。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. p { height: 30px; width: 150px; background-color: #ccf; }
  6. div {height: 30px; width: 150px; background-color: #cfc; }
  7. </style>
  8. <script src="http://code.jquery.com/jquery-latest.js"></script>
  9. </head>
  10. <body>
  11. <p>paragraph</p>
  12. <div>division</div>
  13. <script>
  14. $("p").click(function(event){
  15. event.stopImmediatePropagation();
  16. });
  17. $("p").click(function(event){
  18. // This function won't be executed
  19. $(this).css("background-color", "#f00");
  20. });
  21. $("div").click(function(event) {
  22. // This function will be executed
  23. $(this).css("background-color", "#f00");
  24. });</script>
  25. </body>
  26. </html>

运行一下


还没有评论.