我需要检查嵌套无序列表中的下一个ul是否可见,如果不显示该ul。

我在用:

$('#treemenu1 li a').on('click', function(e) {
    next("ul").is(':hidden').show(300);
    $('#treemenu1 li a').next("ul").is(':visible').hide(300);
});


HTML

<ul id="treemenu1" class="treeview">
    <li><a href="#0">Start</a></li>
    <li><a href="#1">Are your team:</a></li>
    <li class="submenu" id="expandable"><a href="#2">Stakeholder Zen</a>
        <ul class="menu">
            <li><a href="#3">What is a Stakeholder?</a></li>
            <li><a href="#4">Why manage stakeholders?</a></li>
        </ul>
    </li>
    <li class="submenu"><a href="#6">Managing your stakeholders</a>
        <ul class="menu">
            <li><a href="#7">Use four simple steps</a></li>
            <li class="submenu"><a href="#8">Identify your stakeholders</a>
                <ul class="menu">
                    <li><a href="#9">Identification techniques</a></li>
                    <li><a href="#10">Identification tools</a></li>
                </ul>
            </li>
        </ul>

最佳答案

尝试

var $as = $('#treemenu1 li a').on('click', function(e) {
    var $next = $(this).next("ul").toggle(300);
    $as.next("ul:visible").not($next).hide(300);
});


演示:Fiddle

要么

var $as = $('#treemenu1 li a').on('click', function(e) {
    var $next = $(this).next("ul");
    $next.filter(':hidden').stop(true, true).show(300);
    $as.next("ul:visible").not($next).stop(true, true).hide(300);
});


演示:Fiddle

09-18 12:42