[英]Javascript show and hide
How do I make this when I click the header it will hide or show the List? 单击标题将隐藏或显示列表时,该如何做? and when its visible the header shows - and when its not visible it shows a +.
当其可见时,标题显示-,而当其不可见时显示+。 Thanks a lot
非常感谢
<div class="RevealCard">
<h3 class="RevealCard-header">
Top five loves
</h3>
<ol class="RevealCard-list">
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
<li>Fourth item</li>
<li>Fifth item</li>
</ol>
</div>
Here is the CSS 这是CSS
RevealCard-header {
background: #000;
border-radius: 4px 4px 0 0;
color: #fff;
font-size: 1.2em;
font-weight: normal;
margin: 0;
padding: 0.5em;
position: relative;
}
.RevealCard-header::after {
border: 1px solid #fff;
content: "-";
height: 1.15em;
line-height: 1em;
position: absolute;
right: 0.5em;
text-align: center;
width: 1.15em;
}
.RevealCard-list {
border: 1px solid #000;
border-top: none;
margin: 0 0 2em 0;
padding-bottom: 1em;
padding-top: 1em;
}
Here's a working solution: 这是一个可行的解决方案:
var header = document.getElementsByClassName('RevealCard-header')[0]; var list = document.getElementsByClassName('RevealCard-list')[0]; var collapseChar = document.getElementById('collapseChar'); header.addEventListener('click', function(){ // toggle list on click if (list.style.display.toLowerCase() != "none") { // hide list list.style.display = "none"; collapseChar.innerHTML = "+"; } else { // show list list.style.display = "block"; collapseChar.innerHTML = "−"; } })
RevealCard-header { background: #000; border-radius: 4px 4px 0 0; color: #fff; font-size: 1.2em; font-weight: normal; margin: 0; padding: 0.5em; position: relative; } .RevealCard-header::after { border: 1px solid #fff; content: "-"; height: 1.15em; line-height: 1em; position: absolute; right: 0.5em; text-align: center; width: 1.15em; } .RevealCard-list { border: 1px solid #000; border-top: none; margin: 0 0 2em 0; padding-bottom: 1em; padding-top: 1em; } #collapseChar { margin-right: 3px; }
<div class="RevealCard"> <h3 class="RevealCard-header"> <span id="collapseChar">−</span>Top five loves </h3> <ol class="RevealCard-list"> <li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li> <li>Fifth item</li> </ol> </div>
You just have to check to see if the list is visible. 您只需要检查列表是否可见即可。 If it's not, show it;
如果不是,请显示; if it is, hide it.
如果是,请将其隐藏。
It's also here as a fiddle, if you want to play around with it. 如果您想玩弄它的话, 这里也只是摆弄。
This should do it: 应该这样做:
function clickDropdown(){ var listitems = document.getElementById("RevealCard"); var header = document.getElementById("RevealCard-header").innerHTML; if(listitems.style.display == "none"){ listitems.style.display = 'block'; header = header.replace('+', '-'); document.getElementById("RevealCard-header").innerHTML = header; }else{ listitems.style.display = 'none'; header = header.replace('-','+'); document.getElementById("RevealCard-header").innerHTML = header; } }
RevealCard-header { background: #000; border-radius: 4px 4px 0 0; color: #fff; font-size: 1.2em; font-weight: normal; margin: 0; padding: 0.5em; position: relative; } .RevealCard-header::after { border: 1px solid #fff; content: "-"; height: 1.15em; line-height: 1em; position: absolute; right: 0.5em; text-align: center; width: 1.15em; } .RevealCard-list { border: 1px solid #000; border-top: none; margin: 0 0 2em 0; padding-bottom: 1em; padding-top: 1em; }
<div class="RevealCard"> <a onClick="clickDropdown()"> <h3 id="RevealCard-header"> Top five loves + </h3></a> <ol style="display: none;" id="RevealCard"> <li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li> <li>Fifth item</li> </ol> </div>
Not only do you want to toggle the OL element, you also want to change the pseudo ::after css. 您不仅要切换OL元素,还希望更改伪:: after css。 Easiest way?
最简单的方法? Create a second css ::after style, and simply toggle them both.
创建第二个css :: after样式,只需将它们都切换即可。 See below.
见下文。
var myHead = document.getElementsByClassName("RevealCard-header"); for (var i = 0, j = myHead.length; i < j; i++) { myHead[i].addEventListener("click", function() { var myTab = this.parentNode.querySelector("ol"); if (myTab.style.display == "") { // Because of the pseudo ::after el, we'll // create a showing/hiding class. this.classList.remove("header-showing"); this.classList.add("header-hiding"); myTab.style.display = "none" } else { this.classList.remove("header-hiding"); this.classList.add("header-showing"); myTab.style.display = ""; } }) }
.RevealCard { width: 45%; float: left; padding: 5px; } .RevealCard-header { background: #000; border-radius: 4px 4px 0 0; color: #fff; font-size: 1.2em; font-weight: normal; margin: 0; padding: 0.5em; position: relative; } .header-showing::after { border: 1px solid #fff; content: "-"; height: 1.15em; line-height: 1em; position: absolute; right: 0.5em; text-align: center; width: 1.15em; } .header-hiding::after { border: 1px solid #fff; content: "+"; height: 1.15em; line-height: 1em; position: absolute; right: 0.5em; text-align: center; width: 1.15em; } .RevealCard-list { border: 1px solid #000; border-top: none; margin: 0 0 2em 0; padding-bottom: 1em; padding-top: 1em; }
<div class="RevealCard"> <h3 class="RevealCard-header header-showing"> Top five loves </h3> <ol class="RevealCard-list"> <li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li> <li>Fifth item</li> </ol> </div> <div class="RevealCard"> <h3 class="RevealCard-header header-showing"> Another five loves </h3> <ol class="RevealCard-list"> <li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li> <li>Fifth item</li> </ol> </div>
Updated to support multiple sets. 更新以支持多套。 You do this by attaching the listener to the head, then working within its own parent's DOM structure.
您可以通过将侦听器附加到头部,然后在其父级的DOM结构中进行操作来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.