手风琴 - 隐藏已经打开

时间:2018-02-18 02:33:15

标签: javascript css

我想找出一种方法来隐藏我在手风琴中打开的所有子节在单击新标题时。到目前为止,我已经包含了一个jsfiddle。只需要一种方法来关闭打开的部分。谢谢!

HTML

<li onClick={this.handleClick}>Item 1</li>

JS

<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-container">

<h2>Active Accordions</h2>
<p>In this example we add the w3-red class to an active accordion (when the button is clicked on and the content is opened).</p>

<button onclick="myFunction('Demo1')" class="w3-button w3-block w3-black w3-left-align">Open Section 1</button>
<div id="Demo1" class="w3-hide w3-container">
    <p>Some text..</p>
</div>

<button onclick="myFunction('Demo2')" class="w3-button w3-block w3-black w3-left-align">Open Section 2</button>
<div id="Demo2" class="w3-hide w3-container">
    <p>Some other text..</p>
</div>

</div>

</body>
</html>

https://jsfiddle.net/2eda9kz5/1/

1 个答案:

答案 0 :(得分:0)

您需要使用切换,因为它会显示或隐藏匹配的元素。查看jQuery doc:http://api.jquery.com/toggle/

也检查这个答案!它解释了如何关闭所有其他元素但不关闭您单击的元素。 JQuery: Toggle Element that is clicked and hide all other