日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jQuery之防止【冒泡事件】,阻止默认行为 【return false】 event.stopPropagation event.preventDefault...

發布時間:2024/7/19 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery之防止【冒泡事件】,阻止默认行为 【return false】 event.stopPropagation event.preventDefault... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

知識點:

  event.stopPropagation()? 阻止冒泡

  event.preventDefault()?? 阻止默認事件,比如button提交后跳轉到鏈接頁面

  兩者都可以用 return false 代替。

?


?

?

冒泡事件就是點擊子節點,會向上觸發父節點,祖先節點的點擊事件。

下面是html代碼部分:

<body> <div id="content">外層div元素<span>內層span元素</span>外層div元素 </div><div id="msg"></div> </body>

對應的jQuery代碼如下:

<script type="text/javascript"> $(function(){// 為span元素綁定click事件 $('span').bind("click",function(){ var txt = $('#msg').html() + "<p>內層span元素被點擊.<p/>";//獲取html信息 $('#msg').html(txt);// 設置html信息 }); // 為div元素綁定click事件 $('#content').bind("click",function(){ var txt = $('#msg').html() + "<p>外層div元素被點擊.<p/>"; $('#msg').html(txt); }); // 為body元素綁定click事件 $("body").bind("click",function(){ var txt = $('#msg').html() + "<p>body元素被點擊.<p/>"; $('#msg').html(txt); }); }) </script>

當點擊span時,會觸發div與body?的點擊事件。點擊div時會觸發body的點擊事件。

如何防止這種冒泡事件發生呢?

修改如下: 內部阻止冒泡方法:event.stopPropagation();

<script type="text/javascript"> $(function(){// 為span元素綁定click事件 $('span').bind("click",function(event){ var txt = $('#msg').html() + "<p>內層span元素被點擊.<p/>"; $('#msg').html(txt); event.stopPropagation(); // 阻止事件冒泡 }); // 為div元素綁定click事件 $('#content').bind("click",function(event){ var txt = $('#msg').html() + "<p>外層div元素被點擊.<p/>"; $('#msg').html(txt); event.stopPropagation(); // 阻止事件冒泡 }); // 為body元素綁定click事件 $("body").bind("click",function(){ var txt = $('#msg').html() + "<p>body元素被點擊.<p/>"; $('#msg').html(txt); }); }) </script>

event.stopPropagation(); // 阻止事件冒泡

?

有時候點擊提交按鈕會有一些默認事件。比如跳轉到別的界面。但是如果沒有通過驗證的話,就不應該跳轉。這時候可以通過設置event.preventDefault(); //阻止默認行為 ( 表單提交 )。

下面是案例:

<script type="text/javascript"> $(function(){$("#sub").bind("click",function(event){ var username = $("#username").val(); //獲取元素的值,val() 方法返回或設置被選元素的值。 if(username==""){ //判斷值是否為空 $("#msg").html("<p>文本框的值不能為空.</p>"); //提示信息 event.preventDefault(); //阻止默認行為 ( 表單提交 ) } }) }) </script>

html部分:

<body> <form action="test.html"> 用戶名:<input type="text" id="username" /> <br/> <input type="submit" value="提交" id="sub"/> </form> <div id="msg"></div> </body>

還有一種防止默認行為的方法就是return?false。效果一樣。

代碼如下:

<script type="text/javascript"> $(function(){$("#sub").bind("click",function(event){ var username = $("#username").val(); //獲取元素的值 if(username==""){ //判斷值是否為空 $("#msg").html("<p>文本框的值不能為空.</p>"); //提示信息 return false; } }) }) </script>

同理,上面的冒泡事件也可以通過return?false來處理。

<script type="text/javascript"> $(function(){// 為span元素綁定click事件 $('span').bind("click",function(event){ var txt = $('#msg').html() + "<p>內層span元素被點擊.<p/>"; $('#msg').html(txt); return false; }); // 為div元素綁定click事件 $('#content').bind("click",function(event){ var txt = $('#msg').html() + "<p>外層div元素被點擊.<p/>"; $('#msg').html(txt); return false; }); // 為body元素綁定click事件 $("body").bind("click",function(){ var txt = $('#msg').html() + "<p>body元素被點擊.<p/>"; $('#msg').html(txt); }); }) </script>

轉載于:https://www.cnblogs.com/shimily/articles/3939013.html

總結

以上是生活随笔為你收集整理的jQuery之防止【冒泡事件】,阻止默认行为 【return false】 event.stopPropagation event.preventDefault...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。