我使用js設計手風琴菜單,但一次只能打開一個手風琴選項卡。請建議所需的任何修改。我已經嘗試了很多,但是如果有人對此有所瞭解,我沒有得到它,請提出建議。使用js設計的手風琴菜單,但一次只能打開一個手風琴選項卡
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
\t
\t panel.style.maxHeight = null;
} else {
\t panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
height:45px;
border: solid 2px;
border-color: #E5E7E9;
text-align: left;
padding:0 16px ;
outline: none;
font-size: 15px;
transition: 0.4s;
border-radius:10px;
margin: 4px;
}
button.accordion.active, button.accordion:hover {
background-color: #ddd;
}
button.accordion:after {
content: '\002B';
color: #777;
font-weight: bold;
float: right;
margin-left: 5px;
}
button.accordion.active:after {
content: "\2212";
}
div.panel1 {
padding: 0 20px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
border-radius:5px;
}
<div class="container" style="position:relative; margin-top:70px;margin-bottom:70px;" >
<div class="well" style="border-radius:8px;box-shadow:0 0 7px #999 background-color: #fafafa;">
<h2>Our Services</h2>
<button class="accordion"><span class="glyphicon glyphicon-phone" ></span> Mobile Recharge</button>
<div class="panel1">
<p>
<div class="scroll_table" id="style-2">
<table class="table" >
<tbody>
<tr><td>BSNL</td></tr>
<tr><td>Idea</td></tr>
<tr><td>Vodaphone</td></tr>
<tr><td>Reliance</td></tr>
</tbody>
</table>
</div>
</p>
</div>
<button class="accordion"><span class="glyphicon glyphicon-earphone" ></span> Postpaid Bills</button>
<div class="panel1">
<p><div class="scroll_table" id="style-2">
<table class="table" >
<tbody>
<tr><td>BSNL</td></tr>
<tr><td>Idea</td></tr>
<tr><td>Vodaphone</td></tr>
<tr><td>Reliance</td></tr>
</tbody>
</table>
</div>
</p>
</div>
<button class="accordion"><span class="glyphicon glyphicon-phone-alt" ></span> Landline Bills</button>
<div class="panel1">
<p><div class="scroll_table" id="style-2">
<table class="table" >
<tbody>
<tr><td>Airtel Landline and Broadband</td></tr>
<tr><td>BSNL Landline and Broadband</td></tr>
<tr><td>MTNL Delhi</td></tr>
</tbody>
</table>
</div>
</p>
</div>
<button class="accordion"><span class="glyphicon glyphicon-list" ></span> Dth</button>
<div class="panel1">
<p>
<div class="scroll_table" id="style-2">
<table class="table" >
<tbody>
<tr><td>Tata Sky</td></tr>
<tr><td>Dish TV</td></tr>
<tr><td>Sun Direct</td></tr>
<tr><td>Reliance Digital</td></tr>
</tbody>
</table>
</div>
</p>
</div>
<button class="accordion"><span class="glyphicon glyphicon-credit-card" ></span> Datacard</button>
<div class="panel1">
<p>
<div class="scroll_table" id="style-2">
<table class="table" >
<tbody>
<tr><td>Active</td></tr>
<tr><td>Active</td></tr>
<tr><td>Active</td></tr>
<tr><td>Active</td></tr>
</tbody>
</table>
</div>
</p>
</div>
<button class="accordion"><span class="glyphicon glyphicon-flash" ></span> Electricity</button>
<div class="panel1">
<p>
<div class="scroll_table" id="style-2">
<table class="table" >
<tbody>
<tr><td>Marashtra Electricity Board</td></tr>
<tr><td>BSES Rajbhani Power-DELHI</td></tr>
<tr><td>BSES Yamuna Power-DELHI</td></tr>
<tr><td>MSEB Mumbai</td></tr>
</tbody>
</table>
</div>
</p>
</div>
<button class="accordion"><span class="glyphicon glyphicon-fire" ></span> Gas Bill</button>
<div class="panel1">
<p>
<div class="scroll_table" id="style-2">
<table class="table" >
<tbody>
<tr><td>Mahanagar Gas</td></tr>
<tr><td>Active</td></tr>
</tbody>
</table>
</div>
</p>
</div>
<button class="accordion"><span class="glyphicon glyphicon-tint" ></span> Water Bill</button>
<div class="panel1">
<p>
<div class="scroll_table" id="style-2">
<table class="table" >
<tbody>
<tr><td>All Companies</td></tr>
<tr><td>Active</td></tr>
</tbody>
</table>
</div>
</p>
</div>
</div>
</div>
不錯!請注意,如果在頁面的其他地方(其他手風琴,選項卡等)使用泛型「活動」類,則可能會出現問題。 –
我插入上面的代碼,但我得到錯誤:未捕獲TypeError:accArr.forEach不是一個函數 – kkk
哎呀!對不起,我在forEach方法的結尾處遺漏了一個右括號。 (注:我不會推薦在大型數組上使用forEach)林肯,當我開始寫作時,我已經記住了這一點。很好的接收。更新:變量「activeElement」只引用包含「accordion」和「active」類的元素。 (只要她決定在其他元素上添加一個「主動」類) – Lex