2
   $("#nav ul li:not(:has(li.current))")
       .find("ul").hide().end() // Hide all other ULs
       .click(function() {
           $(this).children('ul').slideToggle('fast');
       });

我有上面的代码,它使用嵌套的 ul li,并在单击时扩展级别。但是,我想在第二次点击时收缩关卡,但上述收缩所有关卡,而不仅仅是被点击的关卡。

编辑:例如看这个

4

2 回答 2

2

我不是 100% 确定您打算做什么,但我认为您可以通过检查事件是否源自处理程序元素来避免不必要的滑动:

$(document).ready(function() {
    $("#nav ul li:not(:has(li.current))").find("ul").hide().end() // Hide all other ULs
    .click(function(e) {
        if (this == e.target) {  // if the handler element is where the event originated
            $(this).children('ul').slideToggle('fast');
        }
    });
});

jsFiddle

于 2011-05-25T19:55:03.073 回答
0

您可以只使用 toggle() 并返回 false 来停止传播: Use This Example

于 2011-05-25T19:39:22.483 回答