是的,你可以看看e.target
(改变你的处理程序,以接受e
作为参数),可能使用closest
拿到第一div
祖先实际元素的点击(如果那些孩子div
■找后人)。
$('.parent').bind('click', function(e) {
// Here, `e.target` is the DOM element where the click occurred
var div = $(e.target).closest('div');
});
Live Example | Source
另外,如果你只想的处理程序,以火的时候,点击其中一个孩子,你可以通过delegate
或on
使用事件委派:
$('.parent').delegate('div', 'click', function(e) {
// Here, `this` is the child div that was clicked
});
// or
$('.parent').on('click', 'div', function(e) {
// Here, `this` is the child div that was clicked
});
Live Example | Source
请注意,参数的顺序在delegate
(我更喜欢为了清晰起见)和on
(它看起来每个人都喜欢)之间有所不同。
+1感谢您的详细信息 – 2013-03-02 11:38:56
我更喜欢委托以上,但 - “从jQuery 1.7开始,.delegate()已被.on()方法取代。”链接:http://api.jquery.com/delegate/ - 所以我想这取决于你的JQuery版本,而不是偏好,或者我错了吗?只是好奇。 – 2013-03-02 11:40:17
@Xeano:“取代”并不意味着“弃用”。 '委托'还没有被弃用,甚至在v1.9.2中都没有。 – 2013-03-02 11:41:29