當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。