当前位置: 首页 > 文档资料 > jQuery 入门教程 >

stopImmediatePropagation()

优质
小牛编辑
113浏览
2023-12-01

描述 (Description)

stopImmediatePropagation()方法停止执行其余的处理程序。 此方法还通过调用event.stopPropagation()来停止冒泡。

您可以使用event.isImmediatePropagationStopped()来了解是否曾调用此方法(在该事件对象上)。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>event</i>.stopImmediatePropagation() 

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

  • NA

例子 (Example)

以下是一个简单的例子,简单地显示了这种方法的用法 -

<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("1 - This is : " + $(this).text());
               // Comment the following to see the effect.
               event.stopImmediatePropagation();
            });
            // This won't be executed.
            $("div").click(function(event){
               alert("2 - This is : " + $(this).text());
            });
         });
      </script>
      <style>
         div{ margin:10px;padding:12px; border:2px solid #666; width:160px;}
      </style>
   </head>
   <body>
      <p>Click on any box to see the result:</p>
      <div id = "div1" style = "background-color:blue;">
         BOX 1
      </div>
      <div id = "div2" style = "background-color:red;">
         BOX 2
      </div> 
   </body>
</html>