无涯教程-jQuery - isPropagationStopped()方法函数

86 阅读1分钟

isPropagationStopped()方法检查是否曾经在此事件对象上调用过event.stopPropagation()。

如果已经调用 event.stopPropagation()方法,则此方法返回true,否则返回false。

isPropagationStopped() - 语法

event.isPropagationStopped() 

isPropagationStopped() - 示例

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

<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("This is : " + $(this).text());
               if ( event.isPropagationStopped() ){
                  alert( "Event bubbling is disabled - 1" );
               }else{
                  alert( "Event bubbling is enabled - 1" );
               }
					
               event.stopPropagation();
					
               if ( event.isPropagationStopped() ){
                  alert( "Event bubbling is disabled - 2" );
               }else{
                  alert( "Event bubbling is enabled - 2" );
               }
					
            });
				
         });
      </script>
		
      <style>
         div{ margin:10px;padding:12px; border:2px solid #666; width:160px;}
      </style>
   </head>
	
   <body>
      <p>Click on any box to see the effect:</p>
		
      <div id="div1" style="background-color:blue;">
         OUTER BOX
         <div id="div2" style="background-color:red;">
            INNER BOX
         </div> 
      </div>
   </body>
</html>

这将产生以下输出-

参考链接

www.learnfk.com/jquery/even…