javascript 常用自定义方法

阅读更多

javascript 常用的自定义方法

(1)在页面获取cookie

//get value of cookie  
com.whuang.hsj.getCookie=function(cookieKey){  
    var cookies = document.cookie ? document.cookie.split('; ') : [];  
  
    for (var i = 0, l = cookies.length; i < l; i++) {  
        var parts = cookies[i].split('=');  
        if(parts && parts.length>1){  
            if(parts[0]==cookieKey||com.whuang.hsj.trim(parts[0])==cookieKey){  
                //username1=;  
                return parts[1];  
            }  
        }  
    }  
    return '';  
} ;

 应用场景:

window.onload=function(){
	var loginName22=com.whuang.hsj.$$one("username");
	loginName22.focus();

	//cookie的key是'userEmail'  
    username1=com.whuang.hsj.getCookie('userEmail');  
  
    //alert("username1:"+username1);  
    var issave222=com.whuang.hsj.$$one("issave");  
    if(username1 && username1!='' && username1!=null &&username1!=undefined){  
    	loginName22.value=username1;  
        issave222.checked=true;  
    }else{  
        issave222.checked=false;  
    }  
    
}

 

(2)startWith和endWith

com.whuang.hsj.startWith=function(str,regex){
	return str.indexOf(regex)==0;
};
com.whuang.hsj.startsWith=com.whuang.hsj.startWith;
com.whuang.hsj.endWith=function(str,regex){
	return str.lastIndexOf(regex)==str.length-regex.length;
};
com.whuang.hsj.endsWith=com.whuang.hsj.endWith;

com.whuang.hsj.endWithArray=function(str,array2){
	for ( var i = 0; i < array2.length; i++) {
		var arrOne = array2[i];
		if(com.whuang.hsj.endsWith(str,arrOne)){
			return true;
		}
	}
	return false;
};

 应用场景:上传图片时校验后缀名

var suffix22=['jpg','png','gif','Jpeg'];
		if(!com.whuang.hsj.endWithArray(uploadFile.value,suffix22)){
			alert("后缀名不对");
			return false;
		}

 

(3)设置错误信息(取代alert)

/***
error message
*/
com.whuang.hsj.setErrorMessage=function(obj,spanId,message,isBig){
 	if(obj.focus){//if include focus method
 		obj.focus();
 	}
	 
	 var leaveMessageResultSpan;
	 if (typeof spanId == 'string') {//if argument "spanId" just is a String
		leaveMessageResultSpan=com.whuang.hsj.$$id(spanId);
	 }else{
	 	leaveMessageResultSpan=spanId;
	 }
	 var styleClass;
	 if(isBig){
		 styleClass='errormessageBig';
	 }else{
		 styleClass='errormessage';
	 }
     // leaveMessageResultSpan.innerHTML=""+message+"";
     leaveMessageResultSpan.innerHTML=message;
     leaveMessageResultSpan.className=styleClass;
     function cleanUp22(){
         leaveMessageResultSpan.innerHTML="";
      }
      setTimeout(cleanUp22,6000);
};

 应用场景:校验表单

var username22=com.whuang.hsj.$$one("username");
    /*if(!com.whuang.hsj.isHasValue( username22.value)){
        username22.focus();
        leaveMessageResultSpan.innerHTML="请填写您的姓名.";
                   function cleanUp22(){
                       leaveMessageResultSpan.innerHTML="";
                    }
                    setTimeout(cleanUp22,3000);
                    return false;
    }*/
    var companyObj=com.whuang.hsj.$$one("company");
    if(!com.whuang.hsj.checkNullValue(companyObj,'leaveMessageResult','请填写您的单位名称.')){
        return ;
    }
    if(!com.whuang.hsj.checkNullValue(username22,'leaveMessageResult','请填写您的姓名.')){
    	return ;
    }
    
    var phoneObj=com.whuang.hsj.$$one("email");
    if(!com.whuang.hsj.checkNullValue(phoneObj,'leaveMessageResult','请填写您的电话.')){
        return ;
    }

    var object22=com.whuang.hsj.$$one("object");
    if(!com.whuang.hsj.checkNullValue(object22,'leaveMessageResult','请填写留言主题.')){
    	return ;
    }
    
    var content22=com.whuang.hsj.$$one("content");
    if(!com.whuang.hsj.checkNullValue(content22,'leaveMessageResult','请填写留言内容.')){
    	return ;
    }
    if(content22.value.length<10){
    	com.whuang.hsj.setErrorMessage(content22,"leaveMessageResult","你的留言内容太短了.");
    	return;
    }
    
    if(content22.value.length>5000){
    	com.whuang.hsj.setErrorMessage(content22,"leaveMessageResult","你的留言内容太长了.");
    	return;
    }
    

 
javascript 常用自定义方法_第1张图片
 错误信息过6秒钟会自动消失

 

(4)动态增加下拉框

function showProductSmallClass(osType,smallId,projectPath)
{
	var abc44=function (obj) {
//		alert(obj);
		var jsonObj2=eval("("+obj+")");
		//alert(obj)
		var productSmallClass_div_id= com.whuang.hsj.$$id("productSmallClass_div_id");
		var html="";
		productSmallClass_div_id.innerHTML=html;
	};
	if(projectPath.lastIndexOf('/')!=projectPath.length-1){
		projectPath=projectPath+"/";
	}
	var url2=projectPath+"productSmallClass/json";
	//alert(url2);
	var xmlhw5=new XMLHttpHuangWei(url2, "productItemclassId="+osType,abc44);
	xmlhw5.XMLGetMethod();

}

 

(5)动态增加一个按钮,并指定click响应事件

com.whuang.hsj.addButton = function(parent22, onClickMethod) {
	var newInput = document.createElement("input");
	newInput.type = "button";
	buttonTd.appendChild(newInput);
	newInput.onclick = onClickMethod;
	return newInput;
};

 

(6)通过name获取单个对象

/***
 * if is radio ,please use com.whuang.hsj.$$arr
 * @param name22
 * @returns
 */
com.whuang.hsj.$$one = function(name22) {
	if (com.whuang.hsj.isHasValue(name22)) {
		var names222=document.getElementsByName(name22);
		//alert("names222:"+names222);
		//alert("typeof:"+(typeof names222 ));
		var className=Object.prototype.toString.call(names222);
		var boolean_isArray;
		var ieHtmlCollection='[object HTMLCollection]';
		if(isIEtest)//if browser is IE
		{
                 boolean_isArray=( className=== '[object Object]') ||(className=== ieHtmlCollection) ||names222 instanceof Array ;
		}else
		{
                 boolean_isArray=( className=== '[object Array]') ||(className=== '[object NodeList]'  )||(className==ieHtmlCollection)||names222 instanceof Array||names222 instanceof NodeList;
		}
		if(names222){
             if(boolean_isArray){
                     return names222[0];
             }else{
                     return names222;//why add [0] ??
			}
		}else{
			return "";
		}
	} else {
		return "";
	}
};

 应用场景

var loginName22=com.whuang.hsj.$$one("username");
	if(!com.whuang.hsj.isHasValue(loginName22.value)){
		loginName22.focus();
		var li_info_id=com.whuang.hsj.$$id('li_info');
		li_info_id.innerHTML=" 请填写用户名.";
		function cleanUp22(){
			li_info_id.innerHTML="";
		}
		setTimeout(cleanUp22,4000);
		return false;
	}

(7) 弹出确认提示

/**
 * 
 * @param {Object}
 *            msg
 */
com.whuang.hsj.confirmDelete = function(msg) {
	if (!com.whuang.hsj.isHasValue(msg)) {
		msg = "Are you sure to delete ?";
	}
	var bl = confirm(msg);
	return bl;
};

 应用:

删除

 

(8)判断是否是数字

/***
* whether is digit,1.2 is allowed
*/
com.whuang.hsj.isNumber=function(int22){
	if(!int22 || int22==undefined){
		return false;
	}
	if( int22.match(/^[\d\\.]+$/i)){ 
    return true;
  }else{
    return false;
  }
};
/***
* whether is Integer,1.2 is not allowed
*/
com.whuang.hsj.isInteger=function(int22){
	if(!int22 || int22==undefined){
		return false;
	}
	if( int22.match(/^[\d]+$/i)){ 
    return true;
  }else{
    return false;
  }
};

 应用:

var qrcodeSizeObj=com.whuang.hsj.$$id('qrcodeSize');
	var qrcodeSizeint=qrcodeSizeObj.value;
	if(!com.whuang.hsj.isHasValue(qrcodeSizeint)){
		qrcodeSizeObj.focus();
		alert('请输入像素大小.');
		return ;
	}
	var isDigit=com.whuang.hsj.isInteger(qrcodeSizeint);
	if(!isDigit){
		qrcodeSizeObj.focus();
		alert('请输入数字[100-1000]');
		return;
	}

 

(9)contains方法

contains=function(source,key2){
	var isDownload=(source.indexOf(key2)>-1);
	if(isDownload){
		return true;
	}else{
		return false;
	}
}

 

(10)使用jQuery给div增加子元素

 var widthStr="400px";
        var heightStr="250px";
        var childElement="";
        // alert(childElement);
        $("#loadPanel").html(childElement);

 

(11)使用jQuery给div增加事件

 $("#loadPanel").click(function(){
            hideLoadPanel();
        });

 

(12)使用this获取元素本身

 

function viewBigPic(img333,event){
                    imgSrc=img333.src;
					// alert(imgSrc);
                    var moveTop22 = parseInt(img333.offsetHeight  );
                    var moveLeft22 = parseInt(img333.offsetLeft );
                    // alert(moveTop22+"  ,  "+moveLeft22);
                    showLoadPanel(imgSrc,moveTop22,moveLeft22);
				}

 

 

可以参阅:http://hw1287789687.iteye.com/blog/2124945

http://hw1287789687.iteye.com/blog/2114086

http://hw1287789687.iteye.com/blog/2128476

相关代码见附件

  • javascript 常用自定义方法_第2张图片
  • 大小: 27.6 KB
  • custom.zip (12.7 KB)
  • 下载次数: 2
  • 查看图片附件

你可能感兴趣的:(javascript,startsWith,自定义函数,常用方法,endsWith)