开发者

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

开发者 https://www.devze.com 2022-12-27 02:45 出处:网络
I have this (simplification): $(li).click(function{al开发者_StackOverflowert(\"test\");}); <li>

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

精彩评论

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