CSS (JS) limit page display text character length _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

<nav class= "Cf_nav clearfix" >
<ul>
<li>
<a href= "http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com" title= "Home" >
<div class= "Nav_block" >
<span> Home </span>
<span class= "hover" > Home </span>
</div>
</a>
</li>
<li>
<a href= "Http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/submit" title= "Submission" >
<div class= "Nav_block" >
<span> Contributions </span>
<span class= "Hover" > Submission </span>
</div>
</a>
</li>
<li>
<a href= "Http://lizhuangs.diandian.com/inbox" title= "DMS" >
<div class= "Nav_block" >
<span> DMS </span>
<span class= "hover" > DMS </span>
</div>
</a>
</li>
<li>
<a href= "http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/archive" title= "Archive" >
<div class= "Nav_block" >
<span> Archive </span>
<span class= "hover" > Archive </span>
</div>
</a>
</li>
<li>
<a href= "Http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/rss" title= "subscribe" >
<div class= "Nav_block" >
<span> Subscriptions </span>
<span class= "hover" > Subscription </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:
Copy Code code as follows:

Copyright C by Zhangxinxu v1.0 2009-09-05
Http://www.zhangxinxu.com
/* $ (". Test1"). Wordlimit (); Automatically gets the CSS width to process, if the CSS does not have the width given in the. 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 a label with class 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 ($ (a). Text (). SUBSTRING (0,$ (this). Text (). length-4));
$ (this). HTML ($ (this). html () + ' ... ');
Copythis.remove ();
$ (this). Wordlimit ();
}else{
Copythis.remove (); Clear replication
Return
}
}else{
var maxwidth=num;
if ($ (this). Text (). Length>maxwidth) {
$ (this). 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.