Dead simple Collapsable List Function for deep and

2020-02-02 00:00发布

问题:

I waded through a TON of terrible js "solutions" for how to simply make collapsible nested list(s).

This is what I eventually came up with.

I'm posting here in the hopes the next guy won't have to deal with all the rufuse out there.

Otherwise feel free to add your own! maybe jquery free method if you can manage it?

  • list must be in "proper" format to work

回答1:

css:

ul>li>ul {
    display: none;
}

js/jquery

$('li').click(function(e){
    e.stopPropagation();
    if(this.getElementsByTagName("ul")[0].style.display =="block")
        $(this).find("ul").slideUp();
    else
        $(this).children(":first").slideDown();
});

jfiddle