jquery中防止冒泡事件

來源:互聯網
上載者:User

標籤:script   tor   如何   javascrip   http   lock   資訊   function   技術分享   

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

下面是html代碼部分:

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

對應的jQuery代碼如下:

 1 <script type="text/javascript"> 2 $(function(){ 3     // 為span元素繫結click事件 4     $(‘span‘).bind("click",function(){ 5         var txt = $(‘#msg‘).html() + "<p>內層span元素被點擊.<p/>";//擷取html資訊 6         $(‘#msg‘).html(txt);// 設定html資訊 7     }); 8     // 為div元素繫結click事件 9     $(‘#content‘).bind("click",function(){10         var txt = $(‘#msg‘).html() + "<p>外層div元素被點擊.<p/>";11         $(‘#msg‘).html(txt);12     });13     // 為body元素繫結click事件14     $("body").bind("click",function(){15         var txt = $(‘#msg‘).html() + "<p>body元素被點擊.<p/>";16         $(‘#msg‘).html(txt);17     });18 })19 </script>

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

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

修改如下:

 

<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>

 

jquery中防止冒泡事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.