2017-02-04 75 views
0

我使用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>

回答

0

document.getElementsByClassName可以與多個類使用。

因此,在打開手風琴之前,先查找一個已經打開的號碼爲document.getElementsByClassName("active accordion")。如果存在,使用您現有的邏輯刪除active類並摺疊下一個同級。然後照常開啓新的手風琴。

1

Element.classList.toggle在Internet Explorer中不起作用,但無論如何,您都不會將其用於您想要的操作。

(ES5)

// reference array of elements with class "accordion" 
var accArr = document.getElementsByClassName("accordion"); 
// add event listener to all "accordion" elements to call "clickHandler" 
accArr.forEach(function(elem){ 
    elem.addEventListener('click', clickHandler) 
}) 

function clickHandler(e){ 
    // if element has class "active" remove class 
    if(e.classList.contains('active'){ 
     e.classList.remove('active') 
    // else remove "active" from current "active" elements and add class "active" to the last clicked "accordion" element 
    } else { 
     removeActive(); 
     e.classList.add('active') 
    } 
} 

function removeActive(){ 
    var activeElement = document.getElementsByClassName('accordion active')[0]; 
    activeElement.classList.remove('active'); 
} 
+0

不錯!請注意,如果在頁面的其他地方(其他手風琴,選項卡等)使用泛型「活動」類,則可能會出現問題。 –

+0

我插入上面的代碼,但我得到錯誤:未捕獲TypeError:accArr.forEach不是一個函數 – kkk

+0

哎呀!對不起,我在forEach方法的結尾處​​遺漏了一個右括號。 (注:我不會推薦在大型數組上使用forEach)林肯,當我開始寫作時,我已經記住了這一點。很好的接收。更新:變量「activeElement」只引用包含「accordion」和「active」類的元素。 (只要她決定在其他元素上添加一個「主動」類) – Lex