不打开手风琴菜单的第二个菜单

not open second menu of accordion menu

我正在使用这个手风琴菜单。

JavaScript:

var acc = document.getElementsByClassName("accordion"), i;

for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function(){
        this.classList.toggle("active");
        this.nextElementSibling.classList.toggle("show");
    }
}

Html:

<!DOCTYPE html>
<html>
<head>
<style>
button.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}

button.accordion.active, button.accordion:hover {
    background-color: #ddd;
}

button.accordion:after {
    content: '795';
    font-size: 13px;
    color: #777;
    float: right;
    margin-left: 5px;
}

button.accordion.active:after {
    content: "96";
}

div.panel {
    padding: 0 18px;
    background-color: white;
    max-height: 0;
    overflow: hidden;
    transition: 0.6s ease-in-out;
    opacity: 0;
}

div.panel.show {
    opacity: 1;
    max-height: 500px;
}
</style>
</head>
<body>
    <h2>Accordion with symbols</h2>
    <p>In this example we have added a "plus" sign to each button. When the user clicks on the button, the "plus" sign is replaced with a "minus" sign.</p>
    <button class="accordion">Section 3</button>
    <div id="foo" class="panel">
        <p>
            <button class="accordion">Section 3</button>
            <div id="foo" class="panel">
                <p></p>
            </div>
        </p>
    </div>
</body>
</html>

我需要使用第3节中的第二个菜单。我将此手风琴菜单添加到另一个手风琴菜单中,但它没有打开。任何想法为什么?我该如何解决这个问题?

在下面的 HTML 结构中,我将 P 标记替换为 DIV 标记。 P 元素不能包含像 DIV.

这样的块级元素

参考:How can I put DIV in P? and P tag

<body>
    <h2>Accordion with symbols</h2>
    <p>In this example we have added a "plus" sign to each button. When the user clicks on the button, the "plus" sign is replaced with a "minus" sign.</p>
    <button class="accordion">Section 3</button>
    <div id="foo" class="panel">
        <div>
            <button class="accordion">Section 3</button>
            <div id="foo" class="panel">
                <div>
                  test
                </div>
            </div>
        </div>
    </div>
</body>

点击事件不适用于您原来的 HTML 结构,因为浏览器更改了 DOM 元素并且 DIV 元素已从 P 标签内部移除(解释了原因与上述参考链接)。此 DOM 更改导致我们在检索 nextElementSibling.

时出现问题

如果能解决您的问题,请采纳。