没有打开手风琴菜单的第二个菜单

时间:2016-11-22 07:16:02

标签: javascript jquery jquery-ui accordion jquery-ui-accordion

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

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: '\02795';
    font-size: 13px;
    color: #777;
    float: right;
    margin-left: 5px;
}

button.accordion.active:after {
    content: "\2796";
}

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节中的第二个菜单。我将此手风琴菜单添加到另一个手风琴菜单中,但它没有打开。有什么想法吗?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

在以下HTML结构中,我将P标记替换为DIV标记。 P元素不能包含DIV等块级元素。

参考:How can I put DIV in P?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元素,并且从P标签内部删除了DIV元素(使用上述参考链接解释了原因)。在我们检索nextElementSibling时,此DOM更改导致出现问题。

如果它能解决您的问题,请接受此答案。