开发者

When would one use jQuery isImmediatePropagationStopped() method?

In what situation would one use jQuery's isImmediateP开发者_如何学编程ropagationStopped method? Example on their documentation page does not help.


If you were in a situation with multiple .live() events for example, you'd want to .stopImmediatePropagation() then check it in a following handler, since you've already bubbled up to the same element. Let's take a live example:

$("a").live("click", function(e) {
    alert("Handler 1");
    e.stopImmediatePropagation();
}).live("click", function(e) {
    alert("Handler 2");
});

You can test it here - notice both alerts still fire.

Even though we're stopping propagation immediately, we're listening at a level it doesn't matter, so we actually need to check it:

$("a").live("click", function(e) {
    alert("Handler 1");
    e.stopImmediatePropagation();
}).live("click", function(e) {
    if(e.isPropagationStopped()) return;
    alert("Handler 2");
});

You can test it here - only the first alert fires, the desired result. Depending on your event order, the same situation happens with .delegate(). There are other examples of course, but these are the situations you're at all likely to encounter in normal usage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜