div里面的内容超出自身高度时,显示省略号

本文介绍了一种使用jQuery实现的多行文本省略号效果的方法,通过动态调整文本内容,确保超过指定高度的div容器内的多行文本能够正确显示为省略号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.给DIV设置属性:width: 200px; text-overflow: ellipsis; overflow: hidden; 当div里面的内容总宽度找过 200PX的时候,超出的部分会以“...”的形式显示。

 

2.上面那个案例之适用于单行文本的现实,才会有效。但当div里面的内容出现多行的时候则不能达到预期的效果。下面是解决多行的时候显示“...”的方案。

 

[html] view plain copy
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>  
  6.     <script type="text/javascript">  
  7.     $(function () {  
  8.         $(".figcaption").each(function (i) {  
  9.             var divH = $(this).height();  
  10.             var $p = $("p", $(this)).eq(0);  
  11.             while ($p.outerHeight() > divH) {  
  12.                 $p.text($p.text().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "..."));  
  13.             };  
  14.         });  
  15.         });  
  16.     </script>  
  17.     <style>  
  18.         .figcaption  
  19.         {  
  20.             background: #EEE;  
  21.             width: 410px;  
  22.             height: 3em;  
  23.             margin: 1em;  
  24.         }  
  25.         .figcaption p  
  26.         {  
  27.             margin: 0;  
  28.             line-height: 1.5em;  
  29.         }  
  30.     </style>  
  31. </head>  
  32. <body>  
  33.     <div class="figcaption">  
  34.         <p>  
  35.             You probably can't do it (currently?) without a fixed-width font like Courier. With  
  36.             a fixed-width font every letter occupies the same horizontal space, so you could  
  37.             probably count the letters and multiply the result with the current font size in  
  38.             ems or exs. Then you would just have to test how many letters fit on one line, and  
  39.             then break it up.</p>  
  40.     </div>  
  41. </body>  
  42. </html>  

转载于:https://www.cnblogs.com/wyaocn/p/5830583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值