简体   繁体   English

单击阅读更多后如何在我的代码上添加阅读更少按钮?

[英]How to add read less button on my code after click on read more?

I need some help with my code.我的代码需要一些帮助。

So, I have a "read more" function, but I always have "Read more" text on that button, and I need to make "Read more" - "Read less" text, how can I add "Read less" text to my code, after click?所以,我有一个“阅读更多”function,但我总是在那个按钮上有“阅读更多”文本,我需要制作“阅读更多”-“阅读更少”文本,我怎样才能将“阅读更少”文本添加到我的代码,点击后?

Here is my view:这是我的看法:

<span class="small2" style="max-height: 120px"  class="col-md-6" align="justify" >{{ $review->review_message }}</span>

Here is my script:这是我的脚本:

$(".small2").each(function () {
    text = $(this).text();
    if (text.length > 200) {
        $(this).html(text.substr(0, 100) + '<span class="elipsis">' + text.substr(100) + '</span><a class="elipsis" href="#">Read more</a>' + 'salut');
    }

});
$(".small2 > a.elipsis").click(function (e) {
    e.preventDefault(); //prevent '#' from being added to the url
    $(this).prev('span.elipsis').fadeToggle(500);
});

Please try it:请试一试:

$(".small2 > a.elipsis").click(function (e) {
    e.preventDefault(); //prevent '#' from being added to the url
    $(this).prev('span.elipsis').fadeToggle(500);

    if ( $(this).text() == "Read more" )
      $(this).text('Read less');
    else
      $(this).text('Read more');

});

I've made a snippet for your problem, look at it:我为你的问题做了一个片段,看看它:

 var readMore = true; $(".small2").each(function () { text = $(this).text(); if (text.length > 200) { $(this).html(text.substr(0, 100) + '<span class="elipsis">' + text.substr(100) + '</span><a class="elipsis" href="#"> Read more</a>'); $('span.elipsis').fadeToggle(10); } }); $(".small2 > a.elipsis").click(function (e) { e.preventDefault(); //prevent '#' from being added to the url $('span.elipsis').fadeToggle(500); $('a.elipsis').text(readMore? 'Read less': 'Read more'); readMore =;readMore; });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="small2" style="max-height: 120px" class="col-md-6" align="justify" >sdfjg oshdlbf pisdb fipvsdb fgiphsdb fpvisdb fpihvsbd vspifb dpifhv sdpihfbv asipfhbv asiphfbv aipfsbvpishfv ishafv pias vfphas fvphs dfvp sdpfdpihf pdahvf psahbf jsabf piabfhidbv iphasb pvichasb dpihvab spvi bapifv aspihvdasjfnd[oashvdasdvc ackb vpasihc vpihfc dfhkzjcnv sdz csdkjcba spdjcn aspidbcv asipdb ashid sdfjg oshdlbf pisdb fipvsdb fgiphsdb fpvisdb fpihvsbd vspifb dpifhv sdpihfbv asipfhbv asiphfbv aipfsbvpishfv ishafv pias vfphas fvphs dfvp sdpfdpihf pdahvf psahbf jsabf piabfhidbv iphasb pvichasb dpihvab spvi bapifv aspihvdasjfnd[oashvdasdvc ackb vpasihc vpihfc dfhkzjcnv sdz csdkjcba spdjcn aspidbcv asipdb ashid </span>

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

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