JQuery中event的preventDefault和stopPropagation介紹

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

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                     event.preventDefault();14                     //同樣也支援取消事件冒泡15                     //event.stopPropagation();16                 });17             });18         });19         function output(text) {20             $("#content").append(text + "<br/>");21         }22     </script>23 </head>24 <body id="body">25     <div id="parent">26         <div id="child">27             點擊這裡28             <a id="link" href="http://www.baidu.com">超串連</a>29             <form id="form" action="http://www.baidu.com">30                 <input id="submit" type="submit" value="submit"/>31             </form>32         </div>33     </div>34     <div id="content">35     </div>36 </body>37 </html>
jquery_event05

 

JQuery中event的preventDefault和stopPropagation介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.