標籤: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介紹