JS and CSS Limit page display character length

Source: Internet
Author: User
 <nav class= "Cf_nav clearfix" > <ul> <li> <a href= "Http://lizhu Angs.18021051d350c3871c0a.tpl.diandian.com "title=" Home "> <div class=" Nav_block "> <span> home Page &
    
    lt;/span> <span class= "hover" > Home </span> </div> </a> </li> <li> <a href= "Http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/submit" title= "Submission" > &
        Lt;div class= "Nav_block" > <span> submission </span> <span class= "hover" > Submission </span> </div> </a> </li> <li> <a href= "http://lizhuangs.diandian.co M/inbox "title=" DMS "> <div class=" nav_block "> <span> dms </span> <span class= "hover" > DMs </span> </div> </a> </li> <li> <a HR Ef= "http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/archive "title= Archive" > <div class= "Nav_block" > <span> Archive &
    lt;/span> <span class= "hover" > Archive </span> </div> </a> </li> <li> <a href= "Http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/rss" title= "subscribe" > <div C lass= "Nav_block" > <span> subscriptions </span> <span class= "hover" > Subscribe to </span> ;/div> </a> </li> 

If I'm limiting the characters in the span above.
$ ("Nav_block span"). Wordlimit (8);


It uses the following method to define the jquery plug-in, which I have deducted from the DOT network:

//copyright C by Zhangxinxu v1.0 2009-09-05//http://www.zhangxinxu.com/* $ (". Test1"). Wordlim It (); Automatically gets the CSS width for processing, and if the CSS does not have a width given to. test1, it does not work $ (". Test2"). Wordlimit (24); Intercepts the number of characters, which is an integer greater than 0, where the number of characters in the label class is Test2 is up to 24 */function ($) {$.fn.wordlimit = function (num) {This.each (function () {if (!num)
	{var copythis = $ (This.clonenode (true)). Hide (). css ({' position ': ' absolute ', ' width ': ' Auto ', ' overflow ': ' Visible ')	
	});
	$ (this). After (Copythis);
	if (Copythis.width () >$ (this). Width ()) {$ (this). Text ($ (this). Text (). SUBSTRING (0,$ (this). Text (). length-4));
	$ (this). HTML ($ (this). html () + ' ... ');
	Copythis.remove ();
	$ (this). Wordlimit ();
	}else{copythis.remove ();//clear copy return;
	}}else{var maxwidth=num;
	if ($ (this). Text (). Length>maxwidth) {$ (a). Text ($ (this). Text (). substring (0,maxwidth));
	$ (this). HTML ($ (this). html () + ' ... ');
	}
	}					 
	}); }) (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.