JQuery阻止事件冒泡

來源:互聯網
上載者:User

標籤:htm   bsp   src   新手   value   rip   com   form   jquer   

冒泡事件就是點擊子節點,會向上觸發父節點,祖先節點的點擊事件。我們在平時的開發過程中,肯定會遇到在一個div(這個div可以是元素)包裹一個div的情況,但是呢,在這兩個div上都添加了事件,如果點擊裡面的div我們希望處理這個div的事件,但是呢,我們不希望外層的div的事件也執行,這時候我們就要用到阻止冒泡。

通俗點來說吧,你在家裡看電視,躲在自己的小房間,但是你不希望聲音傳到隔壁父母的耳朵裡,這時候,你可能躲在被窩裡,或者牆壁的隔音效果很好,阻隔聲音可以理解為阻止冒泡。

 

<style>        #content{            width: 140px;            border: 1px solid blue;        }        #msg{            width: 100px;            height: 100px;            margin: 20px;            border: 1px solid red;        }</style>

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


 

  </body>

 

顯示結果

對應的jQuery代碼如下:

 <script type="text/javascript" src="js/jquery-1.8.3.js"></script>    <script type="text/javascript">    $(function(){        // 為內層div綁定click事件        $("#msg").click(function(){            alert("我是小div");        });     // 為外層div元素繫結click事件        $("#content").click(function(){            alert("我是大div");        });        // 為body元素繫結click事件        $("body").click(function(){            alert("我是body");        });    });                   </script>

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

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

修改如下:

<script type="text/javascript" src="js/jquery-1.8.3.js"></script>    <script type="text/javascript">    $(function(){        // 為內層div綁定click事件        $("#msg").click(function(event){            alert("我是小div");            event.stopPropagation();    //  阻止事件冒泡        });     // 為外層div元素繫結click事件        $("#content").click(function(event){            alert("我是大div");            event.stopPropagation();    //  阻止事件冒泡        });        // 為body元素繫結click事件        $("body").click(function(event){            alert("我是body");            event.stopPropagation();    //  阻止事件冒泡        });    });

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

 

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

html部分:

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

<script type="text/javascript" src="js/jquery-1.8.3.js"></script>    <script type="text/javascript">        $(function(){            $("#sub").click(function(event){                //擷取元素的值,val() 方法返回或設定被選元素的值。                var username = $("#username").val();                  //判斷值是否為空白                if(username==""){                     //提示資訊                    //alert("文字框的值不可為空");                    $("#msg").html("<p>文字框的值不可為空.</p>");                   //阻止預設行為 ( 表單提交 )                    event.preventDefault();                  }            });        });    </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.