jQuery slideToggle嵌套列表ul li结构

问题描述

|
   $(\"#nav ul li:not(:has(li.current))\")
       .find(\"ul\").hide().end() // Hide all other ULs
       .click(function() {
           $(this).children(\'ul\').slideToggle(\'fast\');
       });
我上面的代码使用了一个嵌套的ul li,并在单击时扩展了级别。但是,我想在第二次单击时收缩该级别,但是上述收缩了所有级别,而不仅仅是收缩了一个级别。 编辑:例如看这个     

解决方法

我不确定您打算做什么,但我认为您可以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     ,您可以只使用toggle()并返回false来停止传播:
Use This Example
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...