JQuery 提供了两种方式来阻止事件冒泡。


JQuery 提供了两种方式来阻止事件冒泡。

方式一:event.stopPropagation();

        $("#div1").mousedown(function(event){
            event.stopPropagation();
        });

方式二:return false;

        $("#div1").mousedown(function(event){
            return false;
        });

但是这两种方式是有区别的。return false 不仅阻止了事件往上冒泡,而且阻止了事件本身。event.stopPropagation() 则只阻止事件往上冒泡,不阻止事件本身。

checkbox增加阻止事件冒泡

$("input[type='checkbox']").click(function(e){  
            e.stopPropagation();   
        });

 

 

 

 

 

代码示例:

$(function(){
    $('.clickPic').click(function(){
        $(this).children('.bigpic').attr('src','bg0000012112126.png');
        $(this).children('.del').css('display','block');
    })
    
    $('.del').click(function(event){
        event.stopPropagation();
        $('.bigpic').attr('src','bg0000012112125.png');
        $(this).css('display','none');
    })
})

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM