JQuery intercept string plug-ins to distinguish between Chinese and English, jquery Chinese and English
JQuery intercept string plug-ins distinguish between Chinese and English:
The string truncation function is applicable to a large number of websites, such as news lists. Because long-distance news titles may not be appropriate, you must extract strings of the specified length as needed, the following is a jQuery plug-in that can intercept strings of a specified length and distinguish between Chinese and English.
The code example is as follows:
<! DOCTYPE html>
The preceding Code intercepts strings. If a string exceeds the specified length, it intercepts the specified length and replaces it with a ellipsis, the following describes the implementation process of this plug-in:
I. Implementation principle:
The principle is actually very simple. Here is a brief introduction:
1. Add the fixedWidth () method to the jQuery Class Library:
To achieve this, use $. extend () method. This function adds a method directly to the jQuery class. That is to say, the added function static method is not an instance method. for extend () functions, see $. extend () function usage details.
2. Differences between Chinese and English:
The length attribute of a string returns the number of characters in the string. That is to say, an English word in the string is a character, and a Chinese character is also a character. However, an English character and a Chinese character occupy different Memory Spaces. An English character occupies one byte and a Chinese character occupies two bytes. It is determined by the charCodeAt () function. If the returned value is greater than 255, it indicates the Chinese character. Then num is added with 2; otherwise, it is an English character, and num is added with 1. It is easier to intercept, so I will not talk about it here. Just refer to the Code annotations.
Ii. Code comments:
1. (function ($) {}) (jQuery) declares an anonymous function and executes this function. The parameter is jQuery.
2. $. extend ($, {}) to add a static method for the jQuery class library.
3. fixedWidth: function (str, length, char) {}. The method to be added has three parameters. str is the original string to be truncated, length is the length to be truncated, and char is the custom form used as the omitted symbol for exceeding the content. For example, the default state is "... ", this parameter is optional.
4. var str = str. toString (), which is converted to a string.
5. if (! Char) char = "...". If the third parameter is not provided, the default parameter is "...".
6. var num = length-lengthB (str): the length of the character to be intercepted minus the length of the character. After lengthB () function calculation, the Chinese and English characters are distinguished.
7. if (num <0) {}, if it is smaller than 0, that is, the length to be intercepted is smaller than the string length.
8. str = substringB (str, length-lengthB (char) + char, intercept the specified string length, and add a char.
9. return str. If the intercepted length is greater than the string length, the string will be returned as is.
10 function substringB (str, length) {}. This function is used to intercept a string. It has two parameters. The first parameter is the original string to be truncated, the second is the length of the string to be captured, which is the second parameter in the fixedWidth () function minus the length of the replaced character of the omitted string.
11. var num = 0, len = str. length, tenp = ""; declare several variables and assign values.
12. if (len), judge whether the string is a null string.
13. for (var I = 0; I <len; I ++) {}, traverse every character in the string.
14. if (num> length) break, if num is greater than the actual length to be truncated, it will jump out of this loop.
15. if (str. charCodeAt (I)> 255), judge whether the Unicode encoding of the specified index is about 255. if it is greater than Chinese characters, it is an English character.
16. num + = 2, add num to 2.
17. tenp + = str. charAt (I): append the character of the specified index to tenp.
18. else {num ++; tenp + = str. charAt (I)}. If it is an English character, add num to 1 and append the character of the specified index to tenp.
19. return tenp, returns a string.
20. function lengthB (str) {}, declare a function to calculate the length of a string and distinguish between Chinese and English. The principle is not mentioned here, because it is similar to the one described above.
Iii. Related reading:
1. For details about the length attribute, see the length attribute section of the String object in JavaScript.
2. For the charCodeAt () function, see charCodeAt () method 1 of the String object in JavaScript.
3. For more information about the charAt () function, see the charAt () method section of the String object in JavaScript.
The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 9251.
For more information, see: http://www.softwhy.com/jquery/