jQuery實現(xiàn)文字超過1行、2行或規(guī)定的行數(shù)時自動加省略號的方法
更新時間:2018年03月28日 10:16:46 作者:郭浩326
這篇文章主要介紹了jQuery實現(xiàn)文字超過1行、2行或規(guī)定的行數(shù)時自動加省略號的方法,涉及jQuery針對頁面元素屬性動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了jQuery實現(xiàn)文字超過1行、2行或規(guī)定的行數(shù)時自動加省略號的方法。分享給大家供大家參考,具體如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.dbjr.com.cn 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; /*根據(jù)父元素的高度來添加省略號 *可以任意設(shè)置顯示的行數(shù) */ 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>
運行效果:
更多關(guān)于jQuery相關(guān)內(nèi)容可查看本站專題:《jQuery字符串操作技巧總結(jié)》、《jQuery操作xml技巧總結(jié)》、《jQuery擴展技巧總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
您可能感興趣的文章:
- jQuery實現(xiàn)控制文字內(nèi)容溢出用省略號(…)表示的方法
- JQuery 控制內(nèi)容長度超出規(guī)定長度顯示省略號
- Jquery循環(huán)截取字符串的方法(多出的字符串處理成"...")
- jQuery(js)獲取文字寬度(顯示長度)示例代碼
- 用jquery實現(xiàn)輸入框獲取焦點消失文字
- jquery xMarquee實現(xiàn)文字水平無縫滾動效果
- jQuery計算textarea中文字數(shù)(剩余個數(shù))的小程序
- jQuery實現(xiàn)長文字部分顯示代碼
- input 輸入框獲得/失去焦點時隱藏/顯示文字(jquery版)
- jquery實現(xiàn)微博文字輸入框 輸入時顯示輸入字數(shù) 效果實現(xiàn)
相關(guān)文章
詳解jQuery獲取特殊屬性的值以及設(shè)置內(nèi)容
在本篇內(nèi)容里小編給大家分享了jQuery獲取特殊屬性的值以及設(shè)置內(nèi)容相關(guān)知識點,有需要的朋友們參考下。2018-11-11jQuery 快速結(jié)束當前正在執(zhí)行的動畫
要快速結(jié)束一個當前正在執(zhí)行的jquery 動畫時可以在執(zhí)行當前動畫的對象上執(zhí)行 stop(true)方法,下面有個不錯的示例感興趣的朋友不要錯過2013-11-11