简体   繁体   English

使用 Javascript 在网站页面内容中阅读更多内容并减少阅读内容

[英]Read more & Read less in website page content using Javascript

Expected output: i want Read more & Read less click option be at the bottom with all content which include paragraph & list.. (Codepen shows Read more & Read less click option at the top)预期的 output:我希望阅读更多和少读点击选项位于底部,所有内容包括段落和列表。(Codepen 在顶部显示多读和少读点击选项) 在此处输入图像描述 Here what i tried: HTML我在这里尝试过:HTML

<a href="#" class="show_hide" data-content="toggle-text">Read More</a>
<div class="content">Testing
  <ul>
    <li>first</li>
  </ul>
</div>

Javascript: Javascript:

$(document).ready(function () {
  $(".content").hide();
  $(".show_hide").on("click", function () {
    var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
    $(".show_hide").text(txt);
    $(this).next('.content').slideToggle(200);
  });
});

Codepen link for Read more & read less implementation: enter link description here阅读更多和阅读更少实施的 Codepen 链接:在此处输入链接描述

To move Read More link to the bottom and have it work use "prev" instead of "next" method.要将阅读更多链接移至底部并使其正常工作,请使用“prev”而不是“next”方法。

 $(document).ready(function () { $(".content").hide(); $(".show_hide").on("click", function () { var txt = $(".content").is(':visible')? 'Read More': 'Read Less'; $(".show_hide").text(txt); $(this).prev('.content').slideToggle(200); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="content">Testing <ul> <li>first</li> </ul> </div> <a href="#" class="show_hide" data-content="toggle-text">Read More</a>

You can use css for this您可以为此使用 css

.show_hide{
  position:absolute;
  top:100%;
}

Or just put it at the bottom of a container that is what people do或者只是把它放在人们做的容器的底部

Your content class (which gets shown and hidden) contains all of your visible content.您的content class(显示和隐藏)包含所有可见内容。 You just need to include some content that does not have the content class before your a element , something like:您只需要a element 之前包含一些没有content class 的内容,例如:

 $(document).ready(function() { $(".content").hide(); $(".show_hide").on("click", function() { var txt = $(".content").is(':visible')? 'Read More': 'Read Less'; $(".show_hide").text(txt); $(this).next('.content').slideToggle(200); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <h3>Always shown</h3> <p>Some text</p> </div> <a href="#" class="show_hide">Read More</a> <div class="content"> <h3>Sometimes shown</h3> <p>More text</p> </div>

Try this:尝试这个:

 $(document).ready(function () { $(".show_hide").on("click", function () { var txt = $(".content").hasClass('visible')? 'Read More': 'Read Less'; $(".show_hide").text(txt); $(".content").toggleClass("visible"); }); });
 .content { height:90px; overflow:hidden; margin-bottom:10px; }.content.visible { height:auto; overflow:visible; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="content">Testing <ul> <li>first</li> <li>first</li> <li>first</li> <li>first</li> <li>first</li> <li>first</li> <li>first</li> <li>first</li> </ul> </div> <a href="#" class="show_hide" data-content="toggle-text">Read More</a>

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

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