zoukankan      html  css  js  c++  java
  • 扩展一个字符自动长度自动检测的函数

    /**
     * 扩展一个字符自动长度自动检测的函数
     * @param  {[type]}   max      [description]
     * @param  {Function} callback [description]
     * @return {[type]}            [description]
     */
    jQuery.fn.maxLength = function(max, callback) {
    		this.each(function(){
    			var type 	= this.tagName.toLowerCase();
    			var inputType = this.type ? this.type.toLowerCase() : null;
    			if(type == "input" && inputType == "text" || inputType == "password") {
    				this.maxLength 	= max;
    			} else if(type == "textarea") {
    				this.onkeypress = function(e) {
    					var obj = e || event;
    					var keyCode = obj.keyCode;
    					var hasSelection = document.selection ? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
    					return !(this.value.length >= max &&(keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !obj.ctrlKey && !obj.altKey && !hasSelection);
    				};
    				this.onkeyup = function() {
    					if('undefined' != typeof(callback)) {
    						callback(this.value.length);
    					}
    					if(this.value.length > max) {
    						this.value = this.value.substring(0,max);
    					}
    					
    				};
    			}
    		});
    };
    

      

  • 相关阅读:
    DNS
    报文组成
    简单的转义字符
    普通字符
    正则表达式介绍
    Mybatis_HelloWorld
    Mybatis介绍
    基本概念
    EGit应用
    EGit
  • 原文地址:https://www.cnblogs.com/luodao1991/p/3965274.html
Copyright © 2011-2022 走看看