简体   繁体   English

使用jQuery自定义手风琴

[英]Custom Accordion using jQuery

I'm trying to create a custom accordion using jquery, have read through quite a few solution on stackoverflow but I just can't seem to get mine to work, I believe it has to deal with the .siblings. 我正在尝试使用jquery创建自定义手风琴,已经阅读了很多关于stackoverflow的解决方案,但是我似乎无法正常工作,我相信它必须处理.siblings。 Anyway, here is my html code: 无论如何,这是我的html代码:

<div class="menu">
    <ul>
        <li class="current">
            <a href="#" class="current"><img class="icon" src=""/>Dashboard</a>
        </li>
        <li>
            <a href="#"><img class="icon" src=""/>Mail</a>
            <ul class="submenu">
                <li>
                    <a href="#" title="">Write New</a>
                </li>
                <li>
                    <a href="#" title="">Junk</a>
                </li>
                <li>
                    <a href="#" title="">Deleted</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="#"><img class="icon" src=""assets/colors.png""/>Child</a>
            <ul class="submenu">
                <li>
                    <a href="#">Child 1</a>
                </li>
                <li>
                    <a href="#">Child 2</a>
                </li>
                <li>
                    <a href="#">Child 3</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="#"><img class="icon" src=""/>Grid</a>
        </li>
        <li>
            <a href="#"><img class="icon" src=""/>Class</a>
        </li>
    </ul>
</div>

And here is my jQuery code: 这是我的jQuery代码:

$('.menu ul li a').click(function() {

    $(this).next('.submenu').siblings('li').slideUp();
    $(this).next('.submenu').slideToggle();

});

The menu toggles open fine, but when I open one, the others won't close. 菜单切换为打开正常,但是当我打开一个菜单时,其他菜单则不会关闭。

Thanks for your help !! 谢谢你的帮助 !!

The reason why it won't close is because the siblings will look at the same level. 它不会关闭的原因是因为兄弟姐妹的视线相同。 So within the LI element it looks for more LI elements. 因此,在LI元素内,它会寻找更多的LI元素。 You wan't the sibling LI 's from the parent, and close there child .submenu . .submenu来自父级的LI兄弟,然后在.submenu关闭子.submenu

Try this: 尝试这个:

$(this).parent().siblings('li').find('.submenu').slideUp();

you're trying to close the one that you're about to open on the one that is currently open 您正在尝试关闭当前要打开的那张即将要打开的那张

$('.menu ul li a').click(function() { $('。menu ul li a')。click(function(){

$('.current').removeClass('current').next('.submenu').siblings('li').slideUp();
$(this).addClass('current').next('.submenu').slideToggle();

}); });

The whole code will be 整个代码将是

$(this).parent().siblings('li').find('.submenu').slideUp();
$(this).next('.submenu').slideDown();

Just changed toggle to slideDown in last line.... 刚刚将切换到最后一行的slideDown...。

I have one doubt ,every anchor will be clickable using user selector... can we make it only first level anchor clickable .any idea ? 我有一个疑问,每个锚点都可以使用用户选择器进行点击...我们可以仅使它成为一级锚点可点击吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM