jQuery:切换 - 列表

发布于 2024-10-10 03:42:00 字数 834 浏览 0 评论 0原文

这是这里的延续: jQuery:列表在页面上展开 自从我上一篇文章

嗨,

以来,我已经研究了另一个列表并提出了这个: http ://jsbin.com/emaza4/4

如您所见,第一个

  • 元素带有
      子元素(item ' #') 在页面加载时自动打开,其他“父项”保持关闭状态,直到单击其中任何一个。我通过将项目“#”放在“abc”类下并将其余项目放在“xyz”类下来实现此目的。
  • 接下来,我希望能够单击另一个父项,例如项目“AF”,它会自动关闭任何其他打开的父项,包括来自不同类的项目“#”(“abc”而不是“xyz”)。

    在此网站上搜索“toggle”导致我发现: 动态切换 jQuery

    所以我尝试添加到我的代码中像这样: http://jsbin.com/emaza4/3/ 但似乎没有正在工作。

    有人能指出我如何解决这个问题的正确方向吗?提前致谢。 :)

    This is a continuation from here: jQuery: List expands on page load

    Hi,

    Since my previous post, I've worked on another list and come up with this: http://jsbin.com/emaza4/4

    As you may see, the first <li> element with a <ul> child (item '#') opens automatically on page load, and the other "parents" stay closed until any one of them is clicked. I achieved this by putting item '#' under class 'abc' and the rest of the items under class 'xyz'.

    Next, I would like to be able to click on another parent, say, item "A-F" and it automatically closes any other opened parent, including item '#' that's from a different class ('abc' instead of 'xyz').

    Searching "toggle" on this website led me to this: jQuery toggle dynamically

    So I tried adding to my code like so: http://jsbin.com/emaza4/3/ but it doesn't seem to be working.

    Could anybody point me in the right direction as to how to solve this problem? Thanks in advance. :)

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    眼泪也成诗 2024-10-17 03:42:00

    您可以只使用 .toggle('slow') (.hide('slow')),在兄弟姐妹上每个处理程序中单击的

  • 的数量,如下所示:
  • $(this).siblings().children().hide('slow');
    

    您可以测试它在这里

    You can just use the hide-only version of .toggle('slow') (.hide('slow')), on the siblings of the clicked <li> in each of your handlers, like this:

    $(this).siblings().children().hide('slow');
    

    You can test it out here.

    蘸点软妹酱 2024-10-17 03:42:00

    我相信你把问题搞得太复杂了。您所需要的只是单击隐藏所有子菜单并打开相关菜单。例如。

    $('li').click(function (){
      $('ul').hide();
      $(this).find('ul').show();
    });
    

    I believe you make the problem a bit overcomplicated. All you need is onclick hide all the sub-menus and open the relevant one. eg.

    $('li').click(function (){
      $('ul').hide();
      $(this).find('ul').show();
    });
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文