我有多個容器(li)。裏面有鏈接。點擊時,他們需要打開一個匹配的div(並隱藏不匹配的div)。點擊鏈接以顯示容器內的div
我可以通過div循環但無法打開或隱藏它們。
Codepen:https://codepen.io/warddem/pen/kkzrPx
<ul>
<li>
<h3>Market study</h3>
<div>
<a href="#" data-toggle="#div1" class="showSingle">Results</a>
<a href="#" data-toggle="#div2" class="showSingle">Learned</a>
<a href="#" data-toggle="#div3" class="showSingle">Deliverables</a>
<a href="#" data-toggle="#close" class="">Close all</a>
<div id="div1" class="optionDiv">Results: </div>
<div id="div2" class="optionDiv">Learned: </div>
<div id="div3" class="optionDiv">Deliverables: </div>
</div>
</li>
<li>
<h3>Competitive research</h3>
<div>
<a href="#" data-toggle="#div1" class="showSingle">Results</a>
<a href="#" data-toggle="#div2" class="showSingle">Learned</a>
<a href="#" data-toggle="#div3" class="showSingle">Deliverables</a>
<a href="#" data-toggle="#close" class="">Close all</a>
<div id="div1" class="optionDiv">Results: </div>
<div id="div2" class="optionDiv">Learned: </div>
<div id="div3" class="optionDiv">Deliverables: </div>
</div>
</li>
</ul>
jQuery('.showSingle').click(function() {
var idOpen = $(this).siblings("div");
var myIndex = $(this).index();
if (idOpen.is("div")) {
// HIDE ALL DIVS IN PARENT
var divLength = idOpen.get(myIndex).id.length;
for (var i = 0, l = divLength; i < l; i++) {
console.log('divs to hide', idOpen[i]);
//HIDE DOESN'T WORK
//idOpen[i].hide();
}
// HOW TO SHOW LINKED DIV ???
console.log('div id to open: ', idOpen.get(myIndex).id);
}
})
ID必須是唯一的。 – Ouroborus