jQuery 中的 stopImmediatePropagation

首页 / jQuery入门教程 / jQuery 中的 stopImmediatePropagation

stopImmediatePropagation()方法停止执行其余的处理程序。此方法还通过调用event.stopPropagation()来停止冒泡。

您可以使用 event.isImmediatePropagationStopped()来了解是否曾经(在该事件对象上)调用过此方法。

stopImmediatePropagation() - 语法

event.stopImmediatePropagation() 

stopImmediatePropagation() - 示例

以下是一个简单的示例,简单说明了此方法的用法-

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" 
         src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type="text/javascript" language="javascript">
         $(document).ready(function() {
			
            $("div").click(function(event){
               alert("1 - This is : " + $(this).text());
               //Comment the following to see the effect.
               event.stopImmediatePropagation();
            });
				
            //This won't be executed.
            $("div").click(function(event){
               alert("2 - This is : " + $(this).text());
            });
				
         });
      </script>
		
      <style>
         div{ margin:10px;padding:12px; border:2px solid #666; width:160px;}
      </style>
   </head>
	
   <body>
      <p>Click on any box to see the result:</p>
		
      <div id="div1" style="background-color:blue;">
         BOX 1
      </div>
		
      <div id="div2" style="background-color:red;">
         BOX 2
      </div> 
   </body>
</html>

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/jquery/events-stopimmediatepropagation.html

来源:LearnFk无涯教程网

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

数据结构与算法之美 -〔王争〕

如何设计一个秒杀系统 -〔许令波〕

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

Netty源码剖析与实战 -〔傅健〕

操作系统实战45讲 -〔彭东〕

程序员的个人财富课 -〔王喆〕

零基础实战机器学习 -〔黄佳〕

编程高手必学的内存知识 -〔海纳〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)