在jQuery中绑定元素及其子元素 [英] Bind element and its children in jQuery

查看:75
本文介绍了在jQuery中绑定元素及其子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将事件绑定到元素及其子元素.最好的方法是什么?

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

推荐答案

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

这篇关于在jQuery中绑定元素及其子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆