jQuery - 只在前景元素上调用.click()?

时间:2011-03-20 03:43:58

标签: javascript jquery html css

http://jsfiddle.net/waitinforatrain/8AqgU/

该链接中的示例显示了嵌套的有序列表。如果您打开Chrome / Firebug控制台,则可以看到单击子元素也会导致其父元素发生.click()事件。

有没有办法只检测点击的可见前景元素上的.click()?

1 个答案:

答案 0 :(得分:2)

您只需要停止点击的传播:

$('#toc li').click(function(e) {
    console.log ($(this).attr('id'));
    e.stopPropagation();
});

查看this page了解详情