简体   繁体   English

Javascript可折叠菜单(隐藏其他元素)

[英]Javascript Collapsible Menu (hide the other elements)

I have the following working Javascript function: 我有以下工作的Javascript函数:

    function collapsible(zap) {
        if (document.getElementById) {
            var abra = document.getElementById(zap).style;
            if (abra.display == "block") {
                abra.display = "none";
            } else {
                abra.display = "block";
            }
            return false;
        } else {
            return true;
        }
}

When I use the following in html code it displays or hides the "element" div: 当我在html代码中使用以下代码时,它将显示或隐藏“元素” div:

<li><a href="#" onclick="return collapsible('element');">Element</a></li>

Thats working fine. 那很好。 But the problem is, that I want to use the function for multiple links, and then the other elements, that were clicked before, stay, open. 但是问题是,我想将该函数用于多个链接,然后将其他元素(之前单击)保持打开状态。

How can I reprogram the code, so that only one div stays open and the other gets closed if i click on another link? 我如何重新编程代码,以便如果单击另一个链接,只有一个div保持打开状态,而另一个div关闭?

Thanks beforehand! 预先感谢!

The way to go is to create a class(or maybe two), like collapsible and active or open that has this style(display: block or none) and then you working adding or removing the class. 要走的路是创建一个(或两个)类,例如具有这种样式(可折叠或不显示)的可折叠和活动或开放式,然后您可以添加或删除该类。

The logic would be: 逻辑将是:

Links that has the class collapsible when clicked would add the active or open class which would give the behavior that remains opens(or active) by css. 单击时具有可折叠类的链接将添加活动类或打开类,这将使css保持打开(或活动)状态。

If you want to hide others elements you would look for the elements with the class collapsible and then remove the active(or open) class if has any. 如果要隐藏其他元素,则将查找具有可折叠类的元素,然后删除active(或open)类(如果有)。

If you could use jQuery and more importantly jQueryUI accordion I think it would accomplish exactly what you're looking for. 如果您可以使用jQuery,更重要的是可以使用jQueryUI手风琴,我认为它可以完全满足您的需求。

However, without using those two, here is how I would structure it. 但是,如果不使用这两个,这就是我的结构方式。 Like mentioned above, I would use classes to modify the styles of the divs you want shown or hidden. 就像上面提到的,我将使用类来修改要显示或隐藏的div的样式。 Then the js code can just toggle those classes on each of your elements. 然后,js代码可以在每个元素上切换这些类。 The slightly more difficult part (without jquery) is modifying class values since in your final application you may have lots of classes on each div. 稍微困难一点的部分(没有jquery)是修改类的值,因为在最终应用程序中,每个div上可能都有很多类。 This is just a very crude example to get you going. 这只是一个非常粗糙的例子,可以帮助您继续前进。

Working JSFiddle Example 工作JSFiddle示例

Sample DOM 样本DOM

<div >
    <li><a href="#" onclick="return collapsible('elem1');">Element1</a></li>
    <div id='elem1' class='myelem visible'>
        Element 1 contents
</div>
</div>
<div >
    <li><a href="#" onclick="return collapsible('elem2');">Element2</a></li>
    <div id='elem2' class='myelem'>
        Element 2 contents
    </div>
</div>
<div >
    <li><a href="#" onclick="return collapsible('elem3');">Element3</a></li>
    <div id='elem3' class='myelem'>
        Element 3 contents
    </div>
</div>

Sample JS JS样本

window['collapsible'] = function(zap) {
    if (document.getElementById)
    {
        var visDivs = document.getElementsByClassName('visible');
        for(var i = 0; i < visDivs.length; i++)
        {
            visDivs[i].className = visDivs[i].className.replace('visible','');
        }
        document.getElementById(zap).className += " visible";
        return false;
    }
    else
        return true;
}

Sample CSS: 样本CSS:

.myelem {
    display: none;
}
.visible {
    display: block;
}

Here is my solution: http://jsfiddle.net/g5oc0uoq/ 这是我的解决方案: http : //jsfiddle.net/g5oc0uoq/

$('.content').hide();
$('.listelement').on('click', function(){
  if(!($(this).children('.content').is(':visible'))){
    $('.content').slideUp();
    $(this).children('.content').slideDown();
  } else {
    $('.content').slideUp();
  }
});

show() and hide() can be used instead of slideUp() and slideDown() if you have performance issues. 如果存在性能问题,可以使用show()和hide()代替slideUp()和slideDown()。

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

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