jquery implements the interception of a specified length string code:
Intercepting a specified length string operation is used extensively in the construction of a Web site, especially in a news list of this type of operation.
Here is an example of a truncated string code:
<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><Metaname= "Author"content= "http://www.softwhy.com/" /><title>jquery intercepts string manipulation</title><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script> <style>*{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">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">Calculate length length length of string</Div> <DivLimit= "Ten">This way, the optimization is open.</Div> <DivLimit= " a">This side has the optimization very public length length very open length</Div> <DivLimit= " a">Calculate word length length</Div> <DivLimit= "Ten">This side has optimized very side has optimized very side has the optimization very side has the optimization very side has the optimization very</Div></Div></Body></HTML>
The above code implements the function of intercepting a string, here is a brief description of how it achieves this effect:
I. Principle of implementation:
Gets the length of the text in the Div, then compares it with the length specified by the attribute limit and, if the length is exceeded, uses the length of the Intercept, followed by ... Alternative.
Two. Code comments:
1.jquery.fn.limit=function () {}, to extend an instance function for jquery, the jquery object can call this function.
2.var self = $ ("Div[limit]") to obtain a collection of div objects with the limit attribute.
3.self.each (function () {}) allows you to get each object in a collection of Div objects to traverse the specified function once.
4.var objstring = $ (this). Text (), gets the textual content of the DIV element, where this refers to the current div when each () function is carried over.
5.var Objlength = $ (this). Text (). Length to get the current contents of the div in Chinese.
6.var num = $ (this). attr ("Limit"), gets the value of the limit attribute in the Div, which is used as the specified character length.
7.if (Objlength > num) {},div text content is longer than the specified length this executes the specified code.
8.$ (This). attr ("title", objstring), sets the Title property value of the div to the content in the Div.
9.objString = $ (this). Text (objstring.substring (0,num) + "..."), intercept the specified length string, and replace it with an ellipsis.
The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=7872
For more information, refer to: http://www.softwhy.com/jquery/
jquery implements the interception of a specified length string code