Home > Web Front-end > JS Tutorial > js, intercept the length of the string, string, length js intercept the length of the string (Chinese and English)

js, intercept the length of the string, string, length js intercept the length of the string (Chinese and English)

巴扎黑
Release: 2016-11-25 13:58:27
Original
1212 people have browsed it

js intercepts the length of the string. When intercepting, the Chinese is calculated as 2 bytes.

function worldLimit(className, factLength){

$('.'+className).each(function(){
var content = $(this ).text();
var allLength = 0;
for (var i = 0; i < content.length; i++) {
var c = content.charCodeAt(i);
//Single byte plus 1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                                                                                                         1
                                                                                                                                                                                                                                    0; i < content.length; i++) {
        var c = content.charCodeAt(i);
                                                                                                                                                                            subLength++ ;
                                                                                                                                          ;
                                                                                                                                                                                 
             }
}
                $(this).text(subContent);                                                                                                         $(this).

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template