Specifies the length of the string code.
The following code intercepts a string of the specified length implemented by jQuery:
Intercepting a specified length string is widely used in website construction, especially in news lists.
The following is an example of intercepting string code:
<! Doctype html>
The code above implements the string truncation function. The following describes how it achieves this effect:
I. Implementation principle:
Get the length of the text in the div, and compare it with the length specified by the limit attribute. If the length exceeds the limit, the specified length is truncated and replaced.
Ii. Code comments:
1. jQuery. fn. limit = function () {}, used to extend an instance function for jQuery. The jQuery object can call this function.
2. var self = $ ("div [limit]"), used to obtain a set of div objects with the limit attribute.
3. self. each (function () {} allows the obtained div object set to traverse and execute a specified function.
4. var objString = $ (this). text (), get the text content in the div element. Here this refers to when the each () function traverses the current div.
5. var objLength = $ (this). text (). length, get the length of the text content in the current div.
6. var num = $ (this). attr ("limit") to obtain the limit attribute value in the div. The specified character length is used here.
7. if (objLength> num) {}, the text content length in div is greater than the specified length, which executes the specified code.
8. $ (this). attr ("title", objString), set the title attribute value of the div to the content in the div.
9. objString = $ (this). text (objString. substring (0, num) + "..."), truncate the specified length string, and use the ellipsis instead of the excess.
The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 7872.
For more information, see: http://www.softwhy.com/jquery/