`

jQuery 阻止事件冒泡

 
阅读更多
http://www.cnblogs.com/jiqing9006/archive/2012/09/11/2679831.html

function playVideo(channel){
	if(channel == undefined || channel == ""){
		return;
	}
	DPSDK.play(channel);
	window.event.stopPropagation && window.event.stopPropagation();
    window.event.cancelBubble && (window.event.cancelBubble = true);
}



<body>
<div id="content">
    外层div元素
    <span>内层span元素</span>
    外层div元素
</div>

<div id="msg"></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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics