当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JQuery event.isPropagationStopped()用法及代码示例


jQuery中的event.isPropagationStopped()方法用于检查是否调用了对象event.stopPropagation()。如果调用event.stopPropagation(),则返回true,否则返回false。

用法:

event.isPropagationStopped()

参数:它包含强制性的单个参数事件。此参数来自事件绑定函数。


示例1:本示例使用event.isPropagationStopped()方法检查event.stopPropagation()是否被调用。

<!DOCTYPE html> 
<html> 
      
<head> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script> 
      
    <script> 
        $(document).ready(function() { 
            $("button").click(function(event) { 
                event.stopPropagation(); 
                alert("Is event.stopPropagation() called: "  
                        +  event.isPropagationStopped()); 
            }); 
        }); 
    </script> 
</head> 
  
<body> 
    <h1> 
        jQuery event.isPropagationStopped() Method 
    </h1> 
      
    <p> 
        click on button to check if the  
        event.stopPropagation() is called. 
    </p> 
      
    <button>Check</button> 
</body> 
  
</html>

输出:

  • 单击按钮之前:
  • 单击按钮后:

示例2:本示例使用event.isPropagationStopped()方法检查event.stopPropagation()是否被调用。

<!DOCTYPE html> 
<html> 
      
<head> 
    <title> 
        event.isPropagationStopped method 
    </title> 
      
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script> 
</head> 
  
<body> 
    <h1> 
        jQuery event.isPropagationStopped() Method 
    </h1> 
      
    <p>  
        click on button to check if the  
        event.stopPropagation() is called. 
    </p> 
      
    <button>Check</button> 
      
    <div id="GFG"></div> 
       
    <script> 
        function propStopped( event ) { 
            var msg = ""; 
              
            if ( event.isPropagationStopped() ) { 
                msg = "True"; 
            }  
            else { 
                msg = "False"; 
            } 
              
            $( "#GFG" ).append( "<div>" + msg + "</div>" ); 
        } 
           
        $( "button" ).click(function(event) { 
            propStopped( event ); 
            propStopped( event ); 
            event.stopPropagation(); 
            propStopped( event ); 
        }); 
    </script> 
</body> 
  
</html>        

输出:

  • 单击按钮之前:
  • 单击按钮后:


相关用法


注:本文由纯净天空筛选整理自AkshayGulati大神的英文原创作品 jQuery | event.isPropagationStopped() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。