Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

jquery Event.stopPropagation() seems not to work

Am I totally missing what this is supposed to do? I expect that if I call stopPropagation() on an event, handlers for that event won't get triggered on ancestor elements, but the example below isn't working that way (in FireFox 3 at least)..

<script type="text/javascript">
    $("input").live("click", function(event){
        console.log("input click handler called")
        event.stopPropagation()
    });

    $("body").live("click", function(event){
        console.log("body was click handler called. event.isPropagationStopped() returns: " + event.isPropagationStopped());
    })

</script>

 ...

<body>
    <input type="text" >
</body>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Live events don't follow the same event bubbling rules. See the documentation on live event handling.

Quote from reference above:

Live events do not bubble in the traditional manner and cannot be stopped using stopPropagation or stopImmediatePropagation. For example, take the case of two click events - one bound to "li" and another "li a". Should a click occur on the inner anchor BOTH events will be triggered. This is because when a $("li").bind("click", fn); is bound you're actually saying "Whenever a click event occurs on an LI element - or inside an LI element - trigger this click event." To stop further processing for a live event, fn must return false.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...