开发者

jQuery 1.3.2 - Best way to prevent events triggering on lower elements?

I have this (simplification):

$(li).click(function{al开发者_StackOverflowert("test");});

<li>
   <input>
</li>

What's the best way to bind an event to li but not fire when the user clicks on the input element?


Try using the stopPropagation method on the Event object.

$('input').click(function(event) {
    event.stopPropagation();
});

You could also target specific children (or direct children) of li using a combination of selectors:

$('li > input, li strong, li span').click( ... );


One solution could involve using event.target.

In the event handler, use

if( $(event.target).is('input') )
    return;


I think you'll have to manually filter it.

$(li).click(function(e) {
    if ($(e.target).is("input")) {
        alert("Test");
    }
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜