分享原生JavaScript技巧大收集(51~60)
来源:互联网 发布:阿里云搭建游戏服务器 编辑:程序博客网 时间:2024/05/18 09:55
文章来源: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(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); return format;}alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
60、原生JavaScript时间个性化输出功能
/*1、< 60s, 显示为“刚刚”2、>= 1min && < 60 min, 显示与当前时间差“XX分钟前”3、>= 60min && < 1day, 显示与当前时间差“今天 XX:XX”4、>= 1day && < 1year, 显示日期“XX月XX日 XX:XX”5、>= 1year, 显示具体日期“XXXX年XX月XX日 XX:XX” */function timeFormat(time){ var date = new Date(time) , curDate = new Date() , year = date.getFullYear() , month = date.getMonth() + 1 , day = date.getDate() , hour = date.getHours() , minute = date.getMinutes() , curYear = curDate.getFullYear() , curHour = curDate.getHours() , timeStr; if(year < curYear){ timeStr = year +'年'+ month +'月'+ day +'日 '+ hour +':'+ minute; }else{ var pastTime = curDate - date , pastH = pastTime/3600000; if(pastH > curHour){ timeStr = month +'月'+ day +'日 '+ hour +':'+ minute; }else if(pastH >= 1){ timeStr = '今天 ' + hour +':'+ minute +'分'; }else{ var pastM = curDate.getMinutes() - minute; if(pastM > 1){ timeStr = pastM +'分钟前'; }else{ timeStr = '刚刚'; } } } return timeStr;}
- 分享原生JavaScript技巧大收集(51~60)
- 分享原生JavaScript技巧大收集(1~10)
- 分享原生JavaScript技巧大收集(41~50)
- 分享原生JavaScript技巧大收集(61~70)
- 分享原生JavaScript技巧大收集(71~80)验证篇
- 分享原生JavaScript技巧大收集(81~90)
- 分享原生JavaScript技巧大收集(91~100)
- 原生JavaScript技巧大收集
- 原生JavaScript技巧大收集
- 原生JavaScript技巧大收集
- 原生JavaScript技巧大收集
- 原生JavaScript技巧大收集
- 原生JavaScript技巧大收集
- 原生JavaScript技巧大收集(1~10)
- 原生JavaScript技巧大收集(11~20)
- 原生JavaScript技巧大收集(21~30)
- 原生JavaScript技巧大收集100
- 原生JavaScript技巧大收集100(上)
- [原创]让你省写大量重复代码的方法 使用PropertyInfo类 反射获取类 的类型
- 分享原生JavaScript技巧大收集(41~50)
- NodeJS与Mysql的交互
- 【IdeaCentre B3系列机型启动或运行过程中有噪音】
- jQuery Ajax 实例 全解析
- 分享原生JavaScript技巧大收集(51~60)
- java设计模式笔记之适配器
- opencv RGB 颜色 灰色图片显示
- 发生ActionScript错误的解决方法
- //打印出所有的“水仙花数”.
- winform 自动升级用到 (未完待整理)
- 关于新手对接单问题的几大建议
- uva11039-Building designing
- 分享原生JavaScript技巧大收集(61~70)