在jQuery中绑定元素及其子元素

Seb*_*ien 4 jquery children bind

我想将一个事件绑定到一个元素及其子元素.做这个的最好方式是什么?

$(element).bind('click', function(event) {
    doSomething();
});
Run Code Online (Sandbox Code Playgroud)

Jis*_*A P 5

$(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
});
Run Code Online (Sandbox Code Playgroud)