【发布时间】:2012-02-23 20:50:26
【问题描述】:
我有以下标记:
<table>
<tbody>
<tr>
<td>
<a href="javascript:void(0)" class=expand>click me</a>
<span>some other content</span>
</td>
<td>
click me too
</td>
</tr>
</tbody>
</table>
我正在将行的点击处理程序附加到表中:
$('table').on('click', 'tr :not(".expand")', function(evt) {
if (! $(evt.target).is(selector)) {
console.log('event is firing even though target doesnt match the selector!!!');
} else {
console.log('event is firing as expected');
}
});
var selector = $('table').data('events').click[0].selector;
我希望事件仅在源自与tr :not(".expand") 选择器(条件的else 部分)匹配的元素时触发,但无论如何都会触发事件。
如何将回调绑定到 <table> 元素处的事件,使其仅在不是被点击的 .expand 元素时触发?
澄清:如果用户点击<span>some other content</span>,事件处理程序仍应执行
【问题讨论】:
-
我很困惑。因此,您希望 tr 中的所有内容都可点击,但您希望展开元素在被点击后不会冒泡?
-
没错。 expand 元素在其他地方已经有一个点击处理程序,所以我只想处理没有在 expand 元素上的点击。
-
检查我的答案以停止传播展开点击。应该处理你需要的东西。