CSS (js) limits the length of text characters displayed on the page

Source: Internet
Author: User
Tags dot net

Copy codeThe Code is as follows: <nav class = "cf_nav clearfix">
<Ul>
<Li>
<A href = "http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com" title = "home">
<Div class = "nav_block">
<Span> homepage </span>
<Span class = "hover"> homepage </span>
</Div>
</A>
</Li>
<Li>
<A href = "http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/submit" title = "">
<Div class = "nav_block">
<Span> contribution </span>
<Span class = "hover"> contribution </span>
</Div>
</A>
</Li>
<Li>
<A href = "http://lizhuangs.diandian.com/inbox" title = "private message">
<Div class = "nav_block">
<Span> private message </span>
<Span class = "hover"> private message </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 = "subscription">
<Div class = "nav_block">
<Span> subscription </span>
<Span class = "hover"> subscription </span>
</Div>
</A>
</Li>

If I limit the characters in the span above.
$ ("Nav_block span"). wordLimit (8 );
It uses the following method to define the jquery plug-in, which I deducted from the dot net:Copy codeThe Code is as follows: // copyright c by zhangxinxu v1.0 2009-09-05
// Http://www.zhangxinxu.com
/* $ (". Test1"). wordLimit (); the css width is automatically obtained for processing. If no width is specified for. test1 in css, it does not work.
$ (". Test2"). wordLimit (24); number of truncated characters. The value is an integer greater than 0. Here, the number of characters in the tag of class test2 can be 24 at most.
*/
(Function ($ ){
$. Fn. wordLimit = function (num ){
This. each (function (){
If (! Num ){
Var copyThis = certificate (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 ));
Certificate (this).html((this).html () + '...');
CopyThis. remove ();
$ (This). wordLimit ();
} Else {
CopyThis. remove (); // clear the copy
Return;
}
} Else {
Var maxwidth = num;
If ($ (this). text (). length> maxwidth ){
$ (This). text ($ (this). text (). substring (0, maxwidth ));
Certificate (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.