Maison > interface Web > js tutoriel > le corps du texte

新浪微博字数统计 textarea字数统计实现代码_javascript技巧

WBOY
Libérer: 2016-05-16 18:02:56
original
1279 Les gens l'ont consulté
复制代码 代码如下:

/*
* 从新浪微博上抄的
* */
var getLength = (function(){
var trim = function(h) {
try {
return h.replace(/^\s+|\s+$/g, "")
} catch(j) {
return h
}
}
var byteLength = function(b) {
if (typeof b == "undefined") {
return 0
}
var a = b.match(/[^\x00-\x80]/g);
return (b.length + (!a ? 0 : a.length))
};

return function(q, g) {
g = g || {};
g.max = g.max || 140;
g.min = g.min || 41;
g.surl = g.surl || 20;
var p = trim(q).length;
if (p > 0) {
var j = g.min,
s = g.max,
b = g.surl,
n = q;
var r = q.match(/(http|https):\/\/[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+([-A-Z0-9a-z\$\.\+\!\_\*\(\)\/\,\:;@&=\?~#%]*)*/gi) || [];
var h = 0;
for (var m = 0,
p = r.length; m var o = byteLength(r[m]);
if (/^(http:\/\/t.cn)/.test(r[m])) {
continue
} else {
if (/^(http:\/\/)+(weibo.com|weibo.cn)/.test(r[m])) {
h += o } else {
h += o }
}
n = n.replace(r[m], "")
}
return Math.ceil((h + byteLength(n)) / 2)
} else {
return 0
}
}
})();
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!