欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery如何防止這種冒泡事件發(fā)生

 更新時(shí)間:2015年02月27日 11:46:12   投稿:hebedich  
這篇文章主要介紹了Jquery使我們簡(jiǎn)化了禁止冒泡事件的操作,只需幾行代碼,需要的朋友可以參考下

冒泡事件就是點(diǎn)擊子節(jié)點(diǎn),事件會(huì)向上傳遞,最后觸發(fā)父節(jié)點(diǎn),祖先節(jié)點(diǎn)的點(diǎn)擊事件。

html代碼部分:

復(fù)制代碼 代碼如下:

<body>
    <div id="content">
        外層div元素
        <span>內(nèi)層span元素</span>
        外層div元素
    </div>
    <div id="msg"></div>
</body>

jQuery代碼如下:

復(fù)制代碼 代碼如下:

<script type="text/javascript">
$(function(){
    $('span').bind("click",function(){
        var txt = $('#msg').html() + "<p>內(nèi)層span元素被點(diǎn)<p/>";
        $('#msg').html(txt);
    });
    $('#content').bind("click",function(){
        var txt = $('#msg').html() + "<p>外層div元素被點(diǎn)擊<p/>";
        $('#msg').html(txt);
    });
    $("body").bind("click",function(){
        var txt = $('#msg').html() + "<p>body元素被點(diǎn)擊<p/>";
        $('#msg').html(txt);
    });
})
</script>

當(dāng)點(diǎn)擊span時(shí),會(huì)觸發(fā)div與body 的點(diǎn)擊事件。點(diǎn)擊div時(shí)會(huì)觸發(fā)body的點(diǎn)擊事件。

如何防止這種冒泡事件發(fā)生呢?修改如下:

復(fù)制代碼 代碼如下:

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

        有時(shí)候點(diǎn)擊提交按鈕會(huì)有一些默認(rèn)事件。比如跳轉(zhuǎn)到別的界面。但是如果沒(méi)有通過(guò)驗(yàn)證,就不應(yīng)該跳轉(zhuǎn)。這時(shí)候可以通過(guò)設(shè)置event.preventDefault(); 阻止默認(rèn)行為。下面是案例:

復(fù)制代碼 代碼如下:

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

html部分:

復(fù)制代碼 代碼如下:

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

還有一種防止默認(rèn)行為的方法就是return false。效果一樣。代碼如下:

復(fù)制代碼 代碼如下:

<script type="text/javascript">
$(function(){
   $("#sub").bind("click",function(event){
         var username = $("#username").val(); 
         if( username == "" ){
             $("#msg").html("<p>文本框的值不能為空.</p>");
             return false;
         }
   })
})
</script>

同理,上面的冒泡事件也可以通過(guò)return false來(lái)處理。

復(fù)制代碼 代碼如下:

<script type="text/javascript">
$(function(){
    $('span').bind("click",function(event){
        var txt = $('#msg').html() + "<p>內(nèi)層span元素被點(diǎn)<p/>";
        $('#msg').html(txt);
        return false;
    });
    $('#content').bind("click",function(event){
        var txt = $('#msg').html() + "<p>外層div元素被點(diǎn)<p/>";
        $('#msg').html(txt);
        return false;
    });
    $("body").bind("click",function(){
        var txt = $('#msg').html() + "<p>body元素被點(diǎn)<p/>";
        $('#msg').html(txt);
    });
})
</script>

jQuery對(duì)DOM的事件觸發(fā)具有冒泡特性。有時(shí)利用這一特性可以減少重復(fù)代碼,但有時(shí)候我們又不希望事件冒泡。這個(gè)時(shí)候就要阻止 jQuery.Event冒泡。

相關(guān)文章

最新評(píng)論