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


JQuery event.stopImmediatePropagation()用法及代碼示例


event.stopImmediatePropagation()是jQuery中的內置方法,用於停止為所選元素執行其餘事件處理程序。

用法:

event.stopImmediatePropagation()

參數:不需要任何參數。


返回值:此方法返回具有應用更改的所選元素。

示例1:在此方法停止其他彈出框之後,此處隻會出現第一個彈出框。

<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
  </script> 
    <style> 
        body { 
            width: 70%; 
            height: 40%; 
            font-size: 30px; 
            padding: 20px; 
            border: 2px solid green; 
        } 
          
        div { 
            display: block; 
            background-color: lightgrey; 
            padding: 4px; 
        } 
    </style> 
    
    <script> 
        $(document).ready(function() { 
            $("div").click(function(event) { 
                alert("1st event executed"); 
                event.stopImmediatePropagation(); 
            }); 
            $("div").click(function(event) { 
                alert("2nd event executed"); 
            }); 
            $("div").click(function(event) { 
                alert("3rd event executed"); 
            }); 
        }); 
    </script> 
</head> 
  
<body> 
  
    <div>Hello, Welcome to GeeksforGeeks.!</div> 
  
</body> 
  
</html>

輸出:

點擊之前:

單擊後:

示例2:

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
  </script> 
    <style> 
        body { 
            width: 70%; 
            height: 40%; 
            font-size: 30px; 
            padding: 20px; 
            border: 2px solid green; 
        } 
          
        p { 
            display: block; 
            padding: 4px; 
            height: 30px; 
            width: 150px; 
            background-color: lightgrey; 
        } 
          
        div { 
            display: block; 
            padding: 4px; 
            height: 30px; 
            width: 400px; 
            background-color: lightgrey; 
        } 
    </style> 
  
</head> 
  
<body> 
  
    <p>Hello, </p> 
    <div>Welcome to GeeksforGeeks.!</div> 
  
    <script> 
        $("p").click(function(event) { 
            event.stopImmediatePropagation(); 
        }); 
        $("p").click(function(event) { 
            // This function will not executed 
            $(this).css("background-color", "yellow"); 
        }); 
        $("div").click(function(event) { 
            // This function will executed 
            $(this).css("background-color", "green"); 
        }); 
    </script> 
</body> 
  
</html>

輸出:
點擊之前:

單擊“p”和“div”元素後。事件僅對div元素執行:



相關用法


注:本文由純淨天空篩選整理自Sruti Rai大神的英文原創作品 jQuery | event.stopImmediatePropagation() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。