字符统计
关于 onpropertychange 和 oninput
HTML
<h3 class="pro_h3 pt_20">标题<span>(<b id="news_title_limit">0</b>/30字)</span></h3>
<input class="input_text" type="text" name="news_title" id="news_title" value="好呀!" />
CSS
.pro_h3{
font-size:20px;
font-family:"微软雅黑";
color:#115f8e;
}
.pro_h3{
font-size:16px;
color:#888;
font-weight:bolder;
padding-bottom:5px;
}
.pro_h3 span{
font-size:12px;
font-weight:normal;
margin-left:5px;
}
.pro_h3 b{ font-weight:normal;}
.input_text{
width:300px;
border:1px solid #CCC;
padding:8px;
font-family:"微软雅黑";
font-size:14px;
color:#454545;
background-color:#f6f6f6;
box-shadow:inset 0 1px 1px rgba(0,0,0,0.1);
-moz-box-shadow:inset 0 1px 1px rgba(0,0,0,0.1);
-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,0.1);
}
JavaScript
/*
参数:
length:最大长度
ele: 输入对象
callBack: 回调方法,参数len表示当前输入框内容字节数, 方法中的this指向ele对
autoFire: 初使化自动调用一次
*/
function input_max(length, ele, showEle, callBack,autoFire){
if(ele.addEventListener){
//ele.addEventListener('input', change, false);
ele.addEventListener('focus', intervalChange, false);
ele.addEventListener('blur', function(){clearInterval(this.dataFocus); delete this.dataFocus}, false);
}else{
//ele.attachEvent('onpropertychange', function(){if(window.event.propertyName == "value"){alert('a');change()}})
ele.attachEvent('onfocus', intervalChange);
ele.addEventListener('onblur', function(){clearInterval(this.dataFocus); delete this.dataFocus});
}
function intervalChange(){
this.dataFocus = setInterval(change,200);
}
function change(){
var len = Math.ceil(byteLength(ele.value)/2);
len = len <= length ? len : length - len;
callBack.call(ele,showEle,len);
};
function byteLength(b) {
if (typeof b == "undefined") { return 0 }
var a = b.match(/[^\x00-\x80]/g);
return (b.length + (!a ? 0 : a.length))
};
//自动调用一次
if(autoFire){change()};
};
// 回调函数
function input_max_callBack(showEle,len){
var...