【问题标题】:Adding Ellipsis on last line of every fixed height paragraph with overflow-y hidden在每个固定高度段落的最后一行添加省略号,并隐藏溢出-y
【发布时间】:2016-08-20 17:49:21
【问题描述】:

我有大约 10 段描述文本,它具有固定高度,文本溢出 y: hidden。

我已添加 JQuery 脚本以在“每个段落”的“最后一行”添加省略号。

但是,该脚本仅适用于描述文本的第一段。 如何使它适用于其他文本段落?

<div class="description" style="height:85px; overflow-y:hidden;" itemprop="description">
  <a id="a" href="{{ product.url | within: collection }}" itemprop="url"> 
    {{ product.description| strip_html  }}  
  </a>
</div>

<script>
  var $p = $('.description #a');
  var divh = $('.description').height();
  while ($p.outerHeight() > divh) {
  $p.text(function (index, text) {
    return text.replace(/\W*\s(\S)*$/, '...');
  });
}
</script>

【问题讨论】:

    标签: javascript jquery overflow liquid ellipsis


    【解决方案1】:

    把它扔进一个循环。

    $('.description #a').each(function() {
      var $p = $(this);
      var divh = $('.description').height();
      while ($p.outerHeight() > divh) {
        $p.text(function (index, text) {
          return text.replace(/\W*\s(\S)*$/, '...');
        });
      }
    });
    

    这假设您所有的 div 都具有相同的类,并且您的锚点具有相同的 id。如果您需要使其更通用,请执行以下操作:

    $('div a').each(function() {
        //Rest of code here
    });
    

    https://jsfiddle.net/mewcg3zo/1/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-05
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 2014-07-18
      • 2019-03-07
      相关资源
      最近更新 更多