1

我想通过单击来切换一个 DIV 类,但不是在单击该 DIV 内的链接时。

我试过这个,但即使点击链接,它也会切换类:

$("ol.compacted .tl_general:not(a)").click(function () {
    $(this).toggleClass("re-expanded");
});

我能做什么?提前致谢!

4

2 回答 2

9

单击该 div 内的链接时停止事件传播。

$("ol.compacted .tl_general a").click(function (e) {
    e.stopPropagation();
});
于 2012-01-26T19:18:21.113 回答
2

确保事件目标是正确的元素。

$("ol.compacted .tl_general").click(function (e) {
    var $this = $(this);
    if ( $this.is(e.target) ) {
        $this.toggleClass("re-expanded");
    }
});
于 2012-01-26T19:19:25.713 回答