简体   繁体   English

在 div 的特定子项上或多或少地切换显示

[英]Toggle show more or less on particular children of div

I'm actually trying to toggle particular div:nth-child on an Elementor accordion.我实际上是在尝试在 Elementor 手风琴上切换特定的 div:nth-child 。 Though I managed to code it in Javascript, I know there's a shorter and cleaner way to loop this.虽然我设法在 Javascript 中对其进行了编码,但我知道有一种更短、更简洁的循环方式。 Please help.请帮忙。

 .acc-sec:nth-child(5), :nth-child(6), :nth-child(7) { display: none; } #acc-more { cursor: pointer; }
 <script> function showMore() { if (document.querySelector("a").innerHTML == "Show more") { var shownth5 = document.querySelector(".acc-sec:nth-child(5)"); shownth5.style.display = "block"; var shownth6 = document.querySelector(".acc-sec:nth-child(6)"); shownth6.style.display = "block"; var shownth7 = document.querySelector(".acc-sec:nth-child(7)"); shownth7.style.display = "block"; document.querySelector("a").innerHTML = "Show less"; document.querySelector("a").addEventListener('click', showLess); } else { showLess(); } } function showLess() { if (document.querySelector("a").innerHTML == "Show less") { var shownth5 = document.querySelector(".acc-sec:nth-child(5)"); shownth5.style.display = "none"; var shownth6 = document.querySelector(".acc-sec:nth-child(6)"); shownth6.style.display = "none"; var shownth7 = document.querySelector(".acc-sec:nth-child(7)"); shownth7.style.display = "none"; document.querySelector("a").innerHTML = "Show more"; document.querySelector("a").addEventListener('click', showMore); }else { showMore(); } } </script> <div class="acc-sec" onload="loop()"> <div>This is the first div.</div> <div>This is the second div.</div> <div>This is the third div.</div> <div>This is the fourth div.</div> <div id="hidden1">This is the fifth div.</div> <div id="hidden2">This is the sixth div.</div> <div id="hidden3">This is the seventh div.</div> <a id="acc-more" onclick="showMore()">Show more</a> </div>

There is a much easier way in doing this.有一种更简单的方法可以做到这一点。

made small changes and added class instead of id as it will create less code.做了一些小的改动并添加了class而不是id因为它会创建更少的代码。

have a look below看看下面

 document.querySelectorAll(".acc-more").forEach(el=>{ const hidden= el.parentElement.querySelectorAll(".hidden"); el.addEventListener("click", ()=>{ hidden.forEach(h=> h.classList.toggle("hidden")) if (hidden[0].classList.contains("hidden")) el.innerHTML = "Show more"; else el.innerHTML = "Show less"; }); });
 .acc-sec.hidden { display: none; } #acc-more { cursor: pointer; }
 <div class="acc-sec"> <div>This is the first div.</div> <div>This is the second div.</div> <div>This is the third div.</div> <div>This is the fourth div.</div> <div class="hidden">This is the fifth div.</div> <div class="hidden">This is the sixth div.</div> <div class="hidden">This is the seventh div.</div> <a class="acc-more" >Show more</a> </div>

* Updating @Alen.toma js snippet. *更新@Alen.toma js 片段。

Using short-hand if-else for changing the innerText of the link just after toggling the hidden class.在切换hidden的 class 之后,使用简写if-else更改链接的innerText Accept the answer if it helps.如果有帮助,请接受答案。

 document.querySelectorAll(".acc-more").forEach(el => { const hidden = el.parentElement.querySelectorAll(".hidden") const showMoreLess = document.querySelector(".acc-more") el.addEventListener("click", () => { hidden.forEach(h => h.classList.toggle("hidden")) showMoreLess.innerText === "Show more"? showMoreLess.innerText = "Show less": showMoreLess.innerText = "Show more" console.log(showMoreLess) }); });
 .acc-sec.hidden { display: none; } #acc-more { cursor: pointer; }
 <div class="acc-sec"> <div>This is the first div.</div> <div>This is the second div.</div> <div>This is the third div.</div> <div>This is the fourth div.</div> <div class="hidden">This is the fifth div.</div> <div class="hidden">This is the sixth div.</div> <div class="hidden">This is the seventh div.</div> <a class="acc-more">Show more</a> </div>

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

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