Specifies the length of the string code.

Source: Internet
Author: User

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/

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.