Jquery崩溃,如何打开和关闭其他?

Jquery崩溃,如何打开和关闭其他?,jquery,Jquery,如果单击“媒体”,它会向下滑动并显示子链接。然后,我点击“项目”,它也会向下滑动,露出其中的链接。单击“项目”时如何隐藏“媒体” HTML代码 手风琴的逻辑非常简单,下面是一个注释版本: $('#nav .collapse').click(function () { //stores a reference to the `ul` which will be toggled var toToggle = $(this).closest('li').find('ul');

如果单击“媒体”,它会向下滑动并显示子链接。然后,我点击“项目”,它也会向下滑动,露出其中的链接。单击“项目”时如何隐藏“媒体”

HTML代码
手风琴的逻辑非常简单,下面是一个注释版本:

$('#nav .collapse').click(function () {
    //stores a reference to the `ul` which will be toggled
    var toToggle = $(this).closest('li').find('ul');
    //hide the others
    $('#nav ul').not(toToggle).slideUp(200);
    //toggle the clicked one
    toToggle.slideToggle(200);
    return false;
});


通过删除不必要的变量,您可以使代码更短,但这样会失去一些可读性:

$('#nav .collapse').click(function () {
    $('#nav ul').not(
        $(this).closest('li').find('ul').slideToggle(200)
    ).slideUp(200);
    return false;
});

$('#nav .collapse').click(function () {
    //stores a reference to the `ul` which will be toggled
    var toToggle = $(this).closest('li').find('ul');
    //hide the others
    $('#nav ul').not(toToggle).slideUp(200);
    //toggle the clicked one
    toToggle.slideToggle(200);
    return false;
});
$('#nav .collapse').click(function () {
    $('#nav ul').not(
        $(this).closest('li').find('ul').slideToggle(200)
    ).slideUp(200);
    return false;
});