简体   繁体   English

带多个孩子的手风琴菜单(面板)

[英]Accordion Menu with Multiple Children (panels)

I'm very new to coding. 我对编码非常陌生。 I'm trying to create an accordion menu with multiple 'drop down' children panels. 我正在尝试创建具有多个“下拉”子面板的手风琴菜单。 I've followed for the most part this 'how to' from w3schools - https://www.w3schools.com/howto/howto_js_accordion.asp 我大部分时间都遵循w3schools的“操作方法”-https: //www.w3schools.com/howto/howto_js_accordion.asp

The issue I'm having is this uses 'nextElementSibling' when I want to show all the siblings/children of the top menu item. 我遇到的问题是,当我想显示顶部菜单项的所有同级/子级时,会使用“ nextElementSibling”。

Is there some way to easily 'get' all the element siblings? 有什么方法可以轻松地“获取”所有元素同级吗?

Here's the codepen I'm working on - https://codepen.io/kbeats/pen/RYzzdW 这是我正在使用的Codepen- https: //codepen.io/kbeats/pen/RYzzdW

My html for the menu is - 我的菜单HTML是-

<ul id="moduleOneList">
    <li class="tile" id="moduleOneTitle"> Module One
        <li class=subTile id="oneSlideOne"> Title Slide</li> 
        <li class=subTile id="oneSlideTwo"> References </li>
    </li>
</ul>
<ul id="moduleTwolist">
    <li class=tile id="moduleTwoTitle"> Module Two
        <li class=subTile id="twoSlideOne"> Title Slide </li>
        <li class=subTile id="twoSlideTwo"> References </li> 
        <li class=subTile id="twoSlideThree"> Third Slide </li>
    </li>

My CSS 我的CSS

    ul {
    list-style:none;
}



li {
    position: relative;
    left: -40px;
    text-decoration: none;
    display: block;
}


.tile {
    background-color: #74A3EB;
    height: 60px;
    width: 220px;
    border-radius: 10px;
    color: white;
    font-family: lato;
    font-weight: 700;
    font-size: 24px;
    line-height: 60px;
    text-align: center;
    cursor: pointer;
    margin: 10px 2px 2px 10px;
    transition: 0.4s ease-in-out;
}

.active, .tile:hover {
    background-color: #3C72F0; /* change this color */
}

.subTile {
    display: none;
    background-color:#4337DD;
    width: 220px;
    height: 40px;
    border-radius: 10px;
    display: none;
    overflow: hidden;
    color: white;
    font-family: lato;
    text-align: center;
    line-height: 40px;
    font-size: 20px;
    margin: 6px 10px 0px 10px; 
    cursor: default;
    transition: background-color 0.5s ease-in-out;
}



.subTile:hover {
    background-color: #605DE8;
}

And my javascript concerning the accordion menu 和我的关于手风琴菜单的JavaScript

    var acc = document.getElementsByClassName("tile");
var i;

for (i=0;i<acc.length;i++) {
    acc[i].addEventListener("click", function(){
        if(this.classList != "active") {
            this.classList.toggle("active");
        } else {
            this.removeClass("active");
        }


        var panel = $('.tile').nextAll();
        if(panel.style.display == "block"){
            panel.style.display = "none";
        } else {
            panel.style.display = "block";
        }

    });
}

I've tried using 我试过使用

 $('.tile').nextAll(); 

to target all the children, but that doesn't seem to be working. 针对所有孩子,但这似乎行不通。

Here's a working codepen . 这是一个工作中的codepen

1) You're binding the click event to .tile , the .subTile are .tile 's child nodes not siblings. 1)您将click事件绑定到.tile.subTile.tile的子节点而不是兄弟节点。

2) this.classList != "active" would always return true because the classList is title or title active . 2) this.classList != "active"总是返回true因为classList是titletitle active

And it's no need to check before toggle class, so simply use this.classList.toggle("active"); 并且无需在切换类之前进行检查,因此只需使用this.classList.toggle("active");

3) var panel = $('.tile').nextAll(); 3) var panel = $('.tile').nextAll();

panel here is a jQuery object but not a DOM element, you can't change style by using panel.style.display = "..."; panel这里是一个jQuery对象,但不是DOM元素,您不能使用panel.style.display = "...";来更改样式panel.style.display = "..."; .

Since you're using jQuery here, it can be done by using show() or hide() like the following. 由于您在这里使用的是jQuery,因此可以使用show()hide()来完成,如下所示。

// this here is the element that just being clicked,
// $("ul,li", this) equals $(this).find("ul,li")
var $panel = $("ul,li", this); // add a $ sign to remind it's a jQuery object
this.classList.contains("active") ? $panel.show() : $panel.hide();

Note that you might need to comment out .tile 's height: 60px; 请注意,您可能需要注释掉.tileheight: 60px; in the CSS. 在CSS中。

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

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