Use JS and CSS to limit the length of characters displayed on the page

Source: Internet
Author: User
Tags dot net
& Lt; navclass & quot; cf_navclearfix & quot; & gt; & lt; ul & gt; & lt; li & gt; & lt; ahref & quot; http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com & quot; title & quot; homepage & quot; & gt;
  • Homepage
  • Contribution
  • Private Message
  • Archive
  • Subscription
    • Homepage
    • Contribution
    • Private Message
    • Archive
    • Subscription
    • If I limit the characters in the span above. $ ("Nav_block span "). wordLimit (8); it uses the following method to calculate and define the jquery plug-in, which I deducted from the dot net: [css] // copyright c by zhangxinxu v1.0 // http://www.zhangxinxu.com /* $ (". Test1 "). wordLimit (); automatically obtains the css width for processing, if not in css. given the width of test1, $ (". test2 "). wordLimit (24); number of truncated characters. The value is an integer greater than 0. Here, it indicates that the number of characters in the label of class test2 can be 24 at most */(function ($) {$. fn. wordLimit = function (num) {this. each (function () {if (! Num) {var copyThis = (this.clonenode(true+}.hide().css ({'position': 'absolute ', 'width': 'auto', 'overflow': 'visable'}); $ (this ). after (copyThis); if (copyThis. width ()> $ (this ). width () {$ (this ). text ($ (this ). text (). substring (0, $ (this ). text (). length-4); then (this).html((this).html () + '... '); copyThis. remove (); $ (this ). wordLimit ();} else {copyThis. remove (); // Clear copy return;} else {var maxwidth = num; if ($ (this ). text (). length> maxwidth) {$ (this ). text ($ (this ). text (). substring (0, maxwidth); Certificate (this).html((this).html () + '... ') ;}}) ;}} (jQuery); // copyright c by zhangxinxu v1.0 2009-09-05 // http://www.zhangxinxu.com /* $ (". Test1 "). wordLimit (); automatically obtains the css width for processing, if not in css. given the width of test1, $ (". test2 "). wordLimit (24); number of truncated characters. The value is an integer greater than 0. Here, it indicates that the number of characters in the label of class test2 can be 24 at most */(function ($) {$. fn. wordLimit = function (num) {this. each (function () {if (! Num) {var copyThis = (this.clonenode(true+}.hide().css ({'position': 'absolute ', 'width': 'auto', 'overflow': 'visable'}); $ (this ). after (copyThis); if (copyThis. width ()> trim () + '... '); copyThis. remove (); $ (this ). wordLimit ();} else {copyThis. remove (); // Clear copy return;} else {var maxwidth = num; if ($ (this ). text (). length> values () + '... ') ;}}) ;}} (jQuery );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.