简体   繁体   English

如何隐藏一个 div 并显示包含在同一个 div 中的另一个 div?

[英]How to hide a div and display another div that is contained within the same div?

In the code below I would like to hide .demo-box and display .demo-box2 when the .section that contains them is on full display.在下面的代码中,当包含它们的 .section 完全显示时,我想隐藏 .demo-box 并显示 .demo-box2 。 I tried adding style.display = block on .demo-box2 and style.display = none on .demo-box but it is not working.我尝试在 .demo-box2 上添加style.display = block和在 .demo-box 上添加style.display = none但它不起作用。 I'm not sure if there is a working solution on CSS that I can try to resolve this problem.我不确定是否有关于 CSS 的可行解决方案,我可以尝试解决这个问题。

 var Boxlayout = (function() { var wrapper = document.body, sections = Array.from(document.querySelectorAll(".section")), closeButtons = Array.from(document.querySelectorAll(".close-section")), demoBox1 = Array.from(document.querySelectorAll(".demo-box")), demoBox2 = Array.from(document.querySelectorAll(".demo-box2")), expandedClass = "is-expanded", hasExpandedClass = "has-expanded-item"; return { init: init }; function init() { _initEvents(); } function _initEvents() { sections.forEach(function(element) { element.onclick = function() { _openSection(this); }; }); closeButtons.forEach(function(element) { element.onclick = function(element) { element.stopPropagation(); _closeSection(this.parentElement); }; }); } function _openSection(element) { if (!element.classList.contains(expandedClass)) { element.classList.add(expandedClass); wrapper.classList.add(hasExpandedClass); demoBox1.style.display = "none"; demoBox2.style.display = "block"; } } function _closeSection(element) { if (element.classList.contains(expandedClass)) { element.classList.remove(expandedClass); wrapper.classList.remove(hasExpandedClass); demoBox1.style.display = "block"; demoBox2.style.display = "none"; } } })(); Boxlayout.init();
 <main class="main"> <section class="section" id="home"> <div class="close-section">&times;</div> <div class="demo-box">Section 1</div> <div class="demo-box2">home</div> </section> <section class="section"> <div class="close-section">&times;</div> <div class="demo-box">Section 2</div> <div class="demo-box2">about</div> </section> <section class="section"> <div class="close-section">&times;</div> <div class="demo-box">Section 3</div> <div class="demo-box2">portfolio</div> </section> <section class="section" id="contact"> <div class="bg"></div> <div class="close-section">&times;</div> <div class="demo-box">Section 4</div> <div class="demo-box2">contact</div> </section> </main>

I didn't quite understood your question.我不太明白你的问题。 But in the below snippet you could check and run how to expand and close a section "show a div and hide another".但是在下面的代码段中,您可以检查并运行如何展开和关闭“显示一个 div 并隐藏另一个”部分。

 var wrapper = document.body, sections = Array.from(document.querySelectorAll(".section")), closeButtons = Array.from(document.querySelectorAll(".open-close-section")), demoBox1 = Array.from(document.querySelectorAll(".demo-box")), demoBox2 = Array.from(document.querySelectorAll(".demo-box2")), expandedClass = "is-expanded", hasExpandedClass = "has-expanded-item"; closeButtons.forEach((button, i, arr) => { //button is the element, i is the index, arr is closeButtons array button.addEventListener("click", function() { console.log(button.parentElement.classList.contains("is-expandad")); if (button.parentElement.classList.contains("is-expandad")) { demoBox1[i].style.display = "block"; demoBox2[i].style.display = "none"; button.innerHTML = "&darr;"; sections[i].classList.remove("is-expandad"); } else { console.log(demoBox2[i]); demoBox1[i].style.display = "none"; demoBox2[i].style.display = "block"; button.innerHTML = "&times;"; sections[i].classList.add("is-expandad"); } }); });
 .demo-box2 { display: none; }
 <main class="main"> <section class="section" id="home"> <div class="open-close-section">&darr;</div> <div class="demo-box">Section 1</div> <div class="demo-box2">home</div> </section> <section class="section"> <div class="open-close-section">&darr;</div> <div class="demo-box">Section 2</div> <div class="demo-box2">about</div> </section> <section class="section"> <div class="open-close-section">&darr;</div> <div class="demo-box">Section 3</div> <div class="demo-box2">portfolio</div> </section> <section class="section" id="contact"> <div class="bg"></div> <div class="open-close-section">&darr;</div> <div class="demo-box">Section 4</div> <div class="demo-box2">contact</div> </section> </main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM