【发布时间】:2013-09-19 14:56:45
【问题描述】:
我正在尝试制作一个网页,您可以在其中单击按钮以显示和隐藏 div,我现在的问题是;
如何通过单击特定按钮隐藏所有其他 div。
代码Java:
function showHide(divId){
var theDiv = document.getElementById(divId);
if(theDiv.style.display=="none"){
theDiv.style.display="block";
}else{
theDiv.style.display="none";
}
}
代码HTML:
<div id="div" onclick="showHide('divcontent')"> This is the button. </div>
<div id="divcontent"> This is a div to hide and show. </div>
<div id="div2" onclick="showHide('divcontent2')"> This is the button. </div>
<div id="divcontent2"> This is a div to hide and show. </div>
<div id="div3" onclick="showHide('divcontent3')"> This is the button. </div>
<div id="divcontent3"> This is a div to hide and show. </div>
因此,如果您单击 div2,我希望其他的显示为隐藏。
【问题讨论】:
-
看来您的做法是错误的。这是我猜您正在寻找的功能:jQuery Accordion
-
这个问题不需要在imo下投票
-
Accordion 肯定符合要求,但如果他需要在外观和感觉方面进行更多定制,则可能需要独立于 UI 库。
标签: javascript jquery html css onclick