jquery implements the interception of a specified length string code:
Interception of strings is often the operation, can be said to be very frequent, almost any news list form of the site will have this operation, the following is a piece of code to achieve this function, directly apply on it, I hope we can bring some help.
The code is as follows:
<!DOCTYPE HTML><HTML><Head><MetaCharSet=utf-8/><title>Batch register event handler-Ant tribe</title><styletype= "Text/css">* {margin:0;padding:0;font-family:"Song Body", Arial, Helvetica, Sans-serif;}#best{width:300px;Height:200px;Border:1px solid #ccc;margin:60px Auto 0;Line-height:1.6;font-size:14px;padding:10px 0 0 10px}. Blank{font-size:18px;Font-weight:Bold;text-align:Center;padding:20px}</style><Scripttype= "Text/javascript"src= "Http://www.softwhy.com/mytest/jQuery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript">JQuery.fn.limit=function(){ var Self=$("Div[limit]"); Self.each (function(){ varobjstring=$( This). text (); varObjlength=$( This). Text (). length; varNum=$( This). attr ("Limit"); if(Objlength>num) { $( This). attr ("title", objstring); Objstring=$( This). Text (Objstring.substring (0, num)+"..."); } }) } $(function() {$ (document.body). limit ();}) </Script></Head><Body><DivID= "Best"> <DivLimit= " a">Ant Tribe welcome you, hope everyone is happy every day</Div> <DivLimit= "Ten">No one is born to be a master, such as by effort</Div> <DivLimit= " a">Every day is a new one, and everyone should cherish it.</Div> <DivLimit= " a">Only the current time is real, tomorrow will always be tomorrow</Div> <DivLimit= "Ten">The spirit of mutual help and the bosom of sharing are the most valuable</Div></Div></Body></HTML>
The above code can achieve the function of intercepting strings, the code is relatively simple here is not more introduction.
Related reading:
1.[limit] Refer to the [attribute] selector section of jquery .
2.text () refer to the text () method section of jquery .
The 3.length property can be found in the section of the length property of a JavaScript string object .
The 4.attr () function can be found in the attr () method section of jquery .
5.jquery.fn can see what the role of Jquery.fn is in a chapter.
The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9529
For more information, refer to: http://www.softwhy.com/jquery/
JQuery implements the interception of a specified length string code