简体   繁体   English

多个显示和隐藏按钮

[英]Multiple show and hide buttons

I want my show more and hide button to work for multiple text contents, now it's only working for one of them. 我希望显示更多和隐藏按钮可用于多个文本内容,现在仅适用于其中之一。 I have 12 different texts that I want to be able to show and hide with 12 different buttons, like the one in my code. 我想使用12种不同的按钮来显示和隐藏12种不同的文本,例如代码中的一个。 How do I do this? 我该怎么做呢?

 var content = document.getElementById("content"); var button = document.getElementById("show-more"); button.onclick = function() { if (content.className == "open") { //shrink the box content.className = ""; button.innerHTML = "Läs mer"; } else { //expand the box content.className = "open"; button.innerHTML = "Dölj"; } }; 
 <div id="content"> Test </div> <a id="show-more">Läs mer</a> 

It is better to use class than id . 使用class比使用id更好。 I have implemented simple snippet where you can design using just class names. 我实现了一个简单的代码段,您可以在其中仅使用类名进行设计。

 var content = document.getElementById("content"); $(".showHide").on("click", function() { $(this).parent().find(".more").toggle(); if ($(this).parent().find(".more").is(":visible")) { $(this).text("Show less"); } else { $(this).text("Show more"); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="content"> <div class="text"> First text1 <div style="display:none;" class="more">Other text 1</div> <a class="showHide" href="#">Show more</a> </div> <hr /> <div class="text"> First text2 <div style="display:none;" class="more">Other text 2</div> <a class="showHide" href="#">Show more</a> </div> </div> 

Hej Linnéa! HejLinnéa!

IDs should be unique, and if you want multiple occurrences of something, you should use class names. ID应该是唯一的,并且如果要多次出现,则应使用类名。

What I would do is to wrap the links inside of the container divs; 我要做的是将链接包装在容器div中;

<div class="content">
    Content
    <a class="toggle" href="#">Läs mer</a>
</div>
<div class="content">
    Content
    <a class="toggle" href="#">Läs mer</a>
</div>

Then, instead of attaching your event listener to each anchor element, take advantage of event propagation , and add a listener to each content wrapper instead; 然后,不要将事件侦听器附加到每个锚元素,而要利用事件传播 ,然后将侦听器添加到每个内容包装器中;

document.querySelectorAll('.content').forEach(function(contentDiv) {
    contentDiv.onclick = function(e) {
        if(e.target.classList.contains('toggle')) {
            e.target.innerHTML = e.currentTarget.classList.contains('open') ? 'Dölj' : 'Läs mer';
            e.currentTarget.classList.toggle('open');
        }
    }
});

Use document querySelectorAll 使用文档querySelectorAll

 var content = Array.from(document.querySelectorAll(".container")); content.forEach(function(el){ //var content= el.querySelector(".content"); var button = el.querySelector(".show-more"); button.addEventListener("click", function () { el.classList.toggle("open"); el.classList.contains("open") ? (button.innerHTML = "Dölj") : (button.innerHTML = "Läs mer"); }, false) }); 
 .container .content{display: none} .container.open .content{display: block} 
 <section> <article class="container"> <p>Lorem Ipsum is simply dummy</p> <p class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p> <a class="show-more">Läs mer</a> </article> <article class="container"> <p>Lorem Ipsum is simply dummy</p> <p class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p> <a class="show-more">Läs mer</a> </article> <article class="container"> <p>Lorem Ipsum is simply dummy</p> <p class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p> <a class="show-more">Läs mer</a> </article> <article class="container"> <p>Lorem Ipsum is simply dummy</p> <p class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p> <a class="show-more">Läs mer</a> </article> </section> 

You can make it work for multiple paragraphs if you use classes for all elements, and then process it from there as element pairs ( div + corresponding button ). 如果对所有元素使用类,则可以使其适用于多个段落,然后从那里将其作为元素对( div +对应的button )进行处理。

I deliberately kept everything as much as possible the same as your original code, so it would be easy to understand the changes. 我特意将所有内容与原始代码保持尽可能的相同,因此很容易理解所做的更改。 And I added some 'hidden' content so you really see something happening. 我还添加了一些“隐藏”内容,因此您确实可以看到正在发生的事情。

 var contentDivs = document.getElementsByClassName("content"); var buttons = document.getElementsByClassName("show-more"); for (var i = 0; i < contentDivs.length; i++) { // "let" creates locally scoped variables for use in the function. let div = contentDivs[i]; let button = buttons[i]; button.onclick = function() { if (div.className == "open") { //shrink the box div.className = "content"; button.innerHTML = "Read more"; } else { //expand the box div.className = "open"; button.innerHTML = "Close"; } }; } 
 div, a { font-size: 14px; } .content { overflow: hidden; max-height: 18px } 
 <div class="content"> Div 1<br />.....<br />.....<br />..... </div> <a class="show-more">Read more</a> <hr size="1"> <div class="content"> Div 2<br />.....<br />.....<br />..... </div> <a class="show-more">Read more</a> <hr size="1"> 

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

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