日期:2014-05-16  浏览次数:20392 次

分享原生JavaScript技巧大收集(51~60)

文章来源:jquery教程?-?http://www.jq-school.com/Show.aspx?id=305

51、原生JavaScript获取页面可视高度

function getPageViewHeight() {
	var d = document, a = d.compatMode == "BackCompat"
			? d.body
			: d.documentElement;
	return a.clientHeight;
}


52、原生JavaScript跨浏览器添加事件

function addEvt(oTarget,sEvtType,fnHandle){
	if(!oTarget){return;}
	if(oTarget.addEventListener){
		oTarget.addEventListener(sEvtType,fnHandle,false);
	}else if(oTarget.attachEvent){
		oTarget.attachEvent("on" + sEvtType,fnHandle);
	}else{
		oTarget["on" + sEvtType] = fnHandle;
	}
}


53、原生JavaScript跨浏览器删除事件

function delEvt(oTarget,sEvtType,fnHandle){
	if(!oTarget){return;}
	if(oTarget.addEventListener){
		oTarget.addEventListener(sEvtType,fnHandle,false);
	}else if(oTarget.attachEvent){
		oTarget.attachEvent("on" + sEvtType,fnHandle);
	}else{
		oTarget["on" + sEvtType] = fnHandle;
	}
}


54、原生JavaScript去掉url前缀

function removeUrlPrefix(a){
  a=a.replace(/:/g,":").replace(/./g,".").replace(///g,"/");
  while(trim(a).toLowerCase().indexOf("http://")==0){
    a=trim(a.replace(/http:\/\//i,""));
  }
  return a;
}


55、原生JavaScript随机数时间戳

function uniqueId(){
	var a=Math.random,b=parseInt;
	return Number(new Date()).toString()+b(10*a())+b(10*a())+b(10*a());
}


56、原生JavaScript全角半角转换,iCase: 0全到半,1半到全,其他不转化

function chgCase(sStr,iCase){
	if(typeof sStr != "string" || sStr.length <= 0 || !(iCase === 0 || iCase == 1)){
		return sStr;
	}
	var i,oRs=[],iCode;
	if(iCase){/*半->全*/
		for(i=0; i<sStr.length;i+=1){ 
			iCode = sStr.charCodeAt(i);
			if(iCode == 32){
				iCode = 12288;				
			}else if(iCode < 127){
				iCode += 65248;
			}
			oRs.push(String.fromCharCode(iCode)); 
		}		
	}else{/*全->半*/
		for(i=0; i<sStr.length;i+=1){ 
			iCode = sStr.charCodeAt(i);
			if(iCode == 12288){
				iCode = 32;
			}else if(iCode > 65280 && iCode < 65375){
				iCode -= 65248;				
			}
			oRs.push(String.fromCharCode(iCode)); 
		}		
	}		
	return oRs.join("");		
}


57、原生JavaScript确认是否键盘有效输入值

function checkKey(iKey){
	if(iKey == 32 || iKey == 229){return true;}/*空格和异常*/
	if(iKey>47 && iKey < 58){return true;}/*数字*/
	if(iKey>64 && iKey < 91){return true;}/*字母*/
	if(iKey>95 && iKey < 108){return true;}/*数字键盘1*/
	if(iKey>108 && iKey < 112){return true;}/*数字键盘2*/
	if(iKey>185 && iKey < 193){return true;}/*符号1*/
	if(iKey>218 && iKey < 223){return true;}/*符号2*/
	return false;
}


58、原生JavaScript获取网页被卷去的位置

function getScrollXY() {
    return document.body.scrollTop ? {
        x: document.body.scrollLeft,
        y: document.body.scrollTop
    }: {
        x: document.documentElement.scrollLeft,
        y: document.documentElement.scrollTop
    }
}


59、原生JavaScript另一种正则日期格式化函数+调用方法

Date.prototype.format = function(format){ //author: meizz
  var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
  }
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(Reg