javascript面向对象学习 - 输入框最大可输入字符

/* 最大可输入字符 */
     function   watching_limit(option){
              this ._watch = document.getElementById(option.watch); //监听的dom元素
           this   ._tip = document.getElementById(option.tip);              //实时提示_watch_obj的dom
           this   ._limit = option.limit    //限制的字数
           this   ._is_byte = option.is_byte          //一个汉字是否按两个字节来计算
     }
     watching_limit.prototype = {
           get_len:   function (){
                   var   _that =   this ;
                   var   length = 0;
                   if (_that._is_byte == 1){
                        return   _that._watch.value.replace( /[^\x00-\xff]/g , "xx" ).length;   
                }   else {
                        return   _that._watch.value.length;    
                }
           },
           tip:   function (){
                   var   _that =   this ;
                   var   length = _that.get_len();
                   var   left = _that._limit - length;
                   if (left <= 0){
                     
                     _that._watch.value = _that._watch.value.substring(0, _that._limit   )
                }
                _that._tip.innerHTML  =   "还可以输入"   +left+ "个字符"   ;
                
           },
           init:   function (){
                   var   _that =   this ;
                _that._watch.onkeydown =   function (){
                     _that.tip();
                }
                   this ._watch.onkeyup =   function (){
                     _that.tip();
                }
           }
     }
     
      var   obj =   new   watching_limit({watch: "reason_note" ,tip:   "reason_note_tip" ,limit:10,is_byte:0});
     obj.init();

你可能感兴趣的:(javascript面向对象学习 - 输入框最大可输入字符)