关于jquery:jQuery如何使用eventisPropagationStopped方法代码实例

25次阅读

共计 1626 个字符,预计需要花费 5 分钟才能阅读完成。

event.isPropagationStopped() 办法在 jQuery 中用于查看对象 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>

输入如下:

单击按钮之前:

单击按钮后:

更多前端开发相干内容请参考:lsbin – IT 开发技术:https://www.lsbin.com/

查看相干的 jQuery 内容:

  • jQuery 刷新页面:https://www.lsbin.com/2951.html
  • jQuery 抉择文本节点:https://www.lsbin.com/2680.html
  • jQuery 更改占位符文本:https://www.lsbin.com/1756.html

正文完
 0