Bind element and its children in jQuery

走远了吗. 提交于 2019-12-10 09:19:30

问题


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();
});

回答1:


$(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
});



回答2:


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.



来源:https://stackoverflow.com/questions/5728178/bind-element-and-its-children-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!