Restrict the length of characters displayed on the page, which has been favored by many netizens. I am also a fans who have collected some practical skills in their spare time. For more information, see
The Code is as follows:
-
Homepage
Homepage
-
Contribution
Contribution
-
Private Message
Private Message
-
Archive
Archive
-
Subscription
Subscription
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:
The 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 );