Bind element and its children in jQuery

2019-05-07 00:12发布

I'd like to bind an event to an element and its children. What is the best way to do this?

$(element).bind('click', function(event) {
    doSomething();
});

2条回答
看我几分像从前
2楼-- · 2019-05-07 00:28
$(element).bind('click', function(event) {
    doSomething();
}).children().bind("click",function(event){
    // code to handle children click here
    event.stopPropagation(); // if you don't want event to bubble up
});
查看更多
Luminary・发光体
3楼-- · 2019-05-07 00:45

The code you have will do just that.

Look in the event's target field to find out which actual child node saw the event.

查看更多
登录 后发表回答