javascript 输入框字符数提示,超出提示

javascript 输入框字符数提示,超出提示 猛击这里看demo
<!————代码开始———–>
<div><input type=”text” id=”in” onkeyup=”tslx(this);” size=”100″ />
<br /><i id=”alt”></i></div>
<script type=”text/javascript”>
function len(s) {
var l = 0;
var a = s.split(“”);

for (var i=0;i<a.length;i++) {
if (a[i].charCodeAt(0)<299) {
l++;
} else {
l+=2;
}
}
return l;
}
function tslx(t,tl){
/*
t = input this
tl = 字符限制数; 默认 50个字符 25个汉字
*/
tl = tl || 50;
var v = t.value;
var txt = “已经输入” + len(v) + “字符”;
if( len(v) > tl){
txt += “,超出” + (len(v) – tl) + “字符”;
document.getElementById(“alt”).style.color = “red”;
}else{
txt += “,还能输入” + (tl – len(v)) + “字符”;
document.getElementById(“alt”).style.color = “”
}
document.getElementById(“alt”).innerHTML = txt;
}
</script>