zoukankan      html  css  js  c++  java
  • JQuery中event的preventDefault和stopPropagation介绍

    event.preventDefault()阻止默认事件行为的触发。

    event.stopPropagation()防止事件冒泡到DOM树上,也就是不触发的任何前辈元素上的事件处理函数。

     1 <!DOCTYPE html>
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5     <title>取消form表单提交或a打开的超连接</title>
     6     <script src="/js/jquery-1.8.2.js"></script>
     7     <script language="javascript" type="text/javascript">
     8         $(function () {
     9             $("*").each(function (index, item) {
    10                 $(item).bind("click", function (event) {
    11                     output("事件源:" + event.target.id + "," + event.target.tagName + ",事件对象:" + event.currentTarget.id);//DOM2会产生冒泡
    12                     //取消form表单提交或a打开的超连接
    13                     window.alert("begin:" + event.isDefaultPrevented());
    14                     event.preventDefault();
    15                     window.alert("end:" + event.isDefaultPrevented());
    16 
    17                     //同样也支持取消事件冒泡
    18                     window.alert("begin:" + event.isPropagationStopped());
    19                     event.stopPropagation();
    20                     window.alert("end:" + event.isPropagationStopped());
    21                 });
    22             });
    23         });
    24         function output(text) {
    25             $("#content").append(text + "<br/>");
    26         }
    27     </script>
    28 </head>
    29 <body id="body">
    30     <div id="parent">
    31         <div id="child">
    32             点击这里
    33             <a id="link" href="http://www.baidu.com">超连接</a>
    34             <form id="form" action="http://www.baidu.com">
    35                 <input id="submit" type="submit" value="submit"/>
    36             </form>
    37         </div>
    38     </div>
    39     <div id="content">
    40     </div>
    41 </body>
    42 </html>
    jquery_event05
  • 相关阅读:
    Unity热更新06-XLua热补丁-05
    Unity热更新06-XLua热补丁-04
    Unity热更新06-XLua热补丁-03
    Unity热更新06-XLua热补丁-02
    Unity热更新06-XLua热补丁-01
    [SQLite][database disk image is malformed]数据库恢复
    VMware 安装MAC
    c#编写的服务中访问网络位置的共享文件夹
    Android Socket发送信息时闪退
    一个简单的Log类
  • 原文地址:https://www.cnblogs.com/qiyebao/p/4162222.html
Copyright © 2011-2022 走看看