返回值:Objectevent.stopImmediatePropagation()
V1.3jQuery event.stopImmediatePropagation() 方法概述
阻止剩余的事件處理函數(shù)執(zhí)行并且防止事件冒泡到DOM樹上。
除了阻止元素上其它的事件處理函數(shù)的執(zhí)行,這個方法還會通過在內(nèi)部調(diào)用 event.stopPropagation() 來停止事件冒泡。如果僅僅想要停止事件冒泡到前輩元素上,而讓這個元素上的其它事件處理函數(shù)繼續(xù)執(zhí)行,我們可以使用event.stopPropagation() 來代替。
使用 event.isImmediatePropagationStopped() 來確定這個方法是否(在那個事件對象上)調(diào)用過了。
注意:
自從.live()方法處理事件一旦傳播到文檔的頂部,live事件是不可能停止傳播的。同樣地,.delegate() 事件將始終傳播給其中包含的被委托元素;元素上的事件將在被委托事件被調(diào)用的時候執(zhí)行。
示例
描述:
阻止調(diào)用其它事件處理函數(shù)。
代碼:
<!DOCTYPE html>
<html>
<head>
<style> p { height: 30px; width: 150px; background-color: #ccf; }
div {height: 30px; width: 150px; background-color: #cfc; } </style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>paragraph</p>
<div>division</div>
<script>
$("p").click(function(event){
event.stopImmediatePropagation();
}); $("p").click(function(event){
// This function won't be executed
$(this).css("background-color", "#f00"); });
$("div").click(function(event) {
// This function will be executed
$(this).css("background-color", "#f00");
});</script>
</body>
</html>