jQuery实现文字超过1行、2行或规定的行数时自动加省略号的方法

仅仅一月的时间,曾经满眼雪白的棉花地只剩下暗褐色的一片,偶尔星星点点飘几点白色,也暗然无光。人类征服大自然的力量是神奇强大的,这种超乎寻常的功能有时候总让你难以置信。

本文实例讲述了jQuery实现文字超过1行、2行或规定的行数时自动加省略号的方法。分享给大家供大家参考,具体如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>www.haodaima.com jQuery自动添加省略号</title>
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script type="text/javascript">
    $(function () {
      $(".figcaption").each(function (i) {
        var divH = $(this).height();
        var $p = $("p", $(this)).eq(0);
        while ($p.outerHeight() > divH) {
          $p.text($p.text().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "..."));
        };
      });
    });
  </script>
  <style>
    *{
      padding: 0px;
      margin: 0px;
    }
    .figcaption{
      width: 300px;
      height: 50px;
      /*根据父元素的高度来添加省略号
      *可以任意设置显示的行数
      */
      border: 1px solid red;
    }
  </style>
</head>
<body>
  <div class="figcaption">
    <p>
      You probably can't do it (currently?) without a fixed-width font like Courier. With
      a fixed-width font every letter occupies the same horizontal space, so you could
      probably count the letters and multiply the result with the current font size in
      ems or exs. Then you would just have to test how many letters fit on one line, and
      then break it up.</p>
  </div>
</body>
<script>
</script>
</html>

运行效果:

希望本文所述对大家jQuery程序设计有所帮助。

到此这篇关于jQuery实现文字超过1行、2行或规定的行数时自动加省略号的方法就介绍到这了。学的时候就要想着现在和将来怎么去用,否则是浪费时间。更多相关jQuery实现文字超过1行、2行或规定的行数时自动加省略号的方法内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
Nodejs中获取当前函数被调用的行数及文件名详解

JS实现textarea通过换行或者回车把多行数字分割成数组并且去掉数组中空的值

layui使用表格渲染获取行数据的例子

在Bootstrap开发框架中使用dataTable直接录入表格行数据的方法

Layui组件Table绑定行点击事件和获取行数据的方法