首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在与文本相同的行上多读/少读,而不丢失href链接

在与文本相同的行上多读/少读,而不丢失href链接
EN

Stack Overflow用户
提问于 2021-06-03 10:54:39
回答 1查看 56关注 0票数 1

我希望read more / less在文本旁边,而不是像现在这样放在下面,不丢失链接,也不在var内容中使用p

即。var content = $(".aaa“p) .html ();&$(”.aaa p").html(html);

怎么做,我到处都找不到解决方案,总会有一些东西丢失。提前感谢

代码语言:javascript
复制
  jQuery(function($) {
      var show_char = 280;
      var ellipses = "... ";
      var content = $(".aaa").html(); 

      if (content.length > show_char) {
        var a = content.substr(0, show_char);
        var b = content.substr(show_char - content.length);
        var html = a + "<span class='truncated'>" + ellipses + "</span><span class='truncated' style='display:none'>" + b + "</span><a class='read-more' href='#'>Read more</a>";
        $(".aaa").html(html);
      }

      $(".read-more").click(function(e) {
        e.preventDefault();
        $(".read-more").text() == "Read more" ? $(".read-more").text("Read less") : $(".read-more").text("Read more") //change here..
        $(".aaa .truncated ").toggle();
      });
    });
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="aaa">
  <p><a href="#">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
</div>

EN

回答 1

Stack Overflow用户

发布于 2021-06-03 11:41:49

问题是,你处理的是.html,而不是.text,所以你的变量a前面有<p>b后面有</p>,这迫使代码附加一些不需要的<p>标记

在下一个示例中,我假设您可能有多个.aaa元素,因此我决定使用.each遍历所有.aaa元素

代码语言:javascript
复制
jQuery(function($) {
  $('.aaa').each(function(){
    var show_char = 280;
    var ellipses = "... ";
    var content = $(this).html(); 

    if (content.trim().length > show_char) {
      var a = content.trim().substr(3, show_char); // use 3 to avoid <p>
      var b = content.trim().substr(show_char - content.trim().length).replace('</p>' , '');  // replace the last </p>
      var html = a + "<span class='truncated'>" + ellipses + "</span><span class='truncated' style='display:none'>" + b + "</span><a class='read-more' href='#'>Read more</a>";
      // wrap the a into `<p></p>` then append the read more to it
      $(this).html('<p>' + html  + '</p>'); 
    }
  });

  $(".read-more").click(function(e) {
    e.preventDefault();
    $(this).text( ( i , v) => v == "Read more" ? "Read Less" : "Read more"); //change here..
    $(this).closest(".aaa").find(".truncated").toggle();
  });
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="aaa">
  <p><a href="#">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
</div>

<div class="aaa">
  <p><a href="#">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
</div>

始终使用.trim()以避免任何空格

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67814806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档