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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。