Home > Article > Web Front-end > How to use jQuery to automatically add ellipses when text exceeds the specified number of lines
This time I will show you how to use jQuery to automatically add ellipses when the text exceeds the specified number of lines, and how to use jQuery to automatically add ellipsis when the text exceeds the specified number of lines. What are the precautions?, here is the actual combat Let’s take a look at the case.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 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 pH = $(this).height(); var $p = $("p", $(this)).eq(0); while ($p.outerHeight() > pH) { $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> <p 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> </p> </body> <script> </script> </html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use JS to move the left list to the right
How to implement sharing in the WeChat applet The function of returning to the homepage after a page
The above is the detailed content of How to use jQuery to automatically add ellipses when text exceeds the specified number of lines. For more information, please follow other related articles on the PHP Chinese website!