javacsripte常用技巧总结.doc

上传人:自*** 文档编号:124239092 上传时间:2020-03-11 格式:DOC 页数:35 大小:121KB
返回 下载 相关 举报
javacsripte常用技巧总结.doc_第1页
第1页 / 共35页
javacsripte常用技巧总结.doc_第2页
第2页 / 共35页
javacsripte常用技巧总结.doc_第3页
第3页 / 共35页
javacsripte常用技巧总结.doc_第4页
第4页 / 共35页
javacsripte常用技巧总结.doc_第5页
第5页 / 共35页
点击查看更多>>
资源描述

《javacsripte常用技巧总结.doc》由会员分享,可在线阅读,更多相关《javacsripte常用技巧总结.doc(35页珍藏版)》请在金锄头文库上搜索。

1、JavaScript 常用技巧1、原生JavaScript实现字符串长度截取1234567891011121314151617181920functioncutstr(str, len) vartemp;varicount = 0;varpatrn = /x00-xff/;varstrre =;for(vari = 0; i str.length; i+) if(icount len - 1) temp = str.substr(i, 1);if(patrn.exec(temp) =null) icount = icount + 1elseicount = icount + 2strre +=

2、 tempelsebreakreturnstrre +.2、原生JavaScript获取域名主机123456789101112functiongetHost(url) varhost =null;if(typeofurl =undefined|null= url) url = window.location.href;varregex = /w+:/(/*).*/;varmatch = url.match(regex);if(typeofmatch !=undefined&null!= match) host = match1;returnhost;3、原生JavaScript清除空格1234

3、String.prototype.trim =function() varreExtraSpace = /s*(.*?)s+$/;returnthis.replace(reExtraSpace,$1)4、原生JavaScript替换全部123String.prototype.replaceAll =function(s1, s2) returnthis.replace(newRegExp(s1,gm), s2)5、原生JavaScript转义html标签123functionHtmlEncode(text) returntext.replace(/&/g,&).replace(/g, ).re

4、place(/g,/g,)6、原生JavaScript还原html标签123functionHtmlDecode(text) returntext.replace(/&/g,&).replace(/g,).replace(/g,/g,)7、原生JavaScript时间日期格式转换123456789101112131415161718Date.prototype.Format =function(formatStr) varstr = formatStr;varWeek = 日,一,二,三,四,五,六;str = str.replace(/yyyy|YYYY/,this.getFullYear(

5、);str = str.replace(/yy|YY/, (this.getYear() % 100) 9 ? (this.getYear() % 100).toString() :0+ (this.getYear() % 100);str = str.replace(/MM/, (this.getMonth() + 1) 9 ? (this.getMonth() + 1).toString() :0+ (this.getMonth() + 1);str = str.replace(/M/g, (this.getMonth() + 1);str = str.replace(/w|W/g, We

6、ekthis.getDay();str = str.replace(/dd|DD/,this.getDate() 9 ?this.getDate().toString() :0+this.getDate();str = str.replace(/d|D/g,this.getDate();str = str.replace(/hh|HH/,this.getHours() 9 ?this.getHours().toString() :0+this.getHours();str = str.replace(/h|H/g,this.getHours();str = str.replace(/mm/,t

7、his.getMinutes() 9 ?this.getMinutes().toString() :0+this.getMinutes();str = str.replace(/m/g,this.getMinutes();str = str.replace(/ss|SS/,this.getSeconds() 9 ?this.getSeconds().toString() :0+this.getSeconds();str = str.replace(/s|S/g,this.getSeconds();returnstr8、原生JavaScript判断是否为数字类型12345678functioni

8、sDigit(value) varpatrn = /0-9*$/;if(patrn.exec(value) =null| value =) returnfalseelsereturntrue9、原生JavaScript设置cookie值123456789functionsetCookie(name, value, Hours) vard =newDate();varoffset = 8;varutc = d.getTime() + (d.getTimezoneOffset() * 60000);varnd = utc + (3600000 * offset);varexp =newDate(n

9、d);exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);document.cookie = name +=+ escape(value) +;path=/;expires=+ exp.toGMTString() +;domain=;10、原生JavaScript获取cookie值12345functiongetCookie(name) vararr = document.cookie.match(newRegExp(| )+ name +=(;*)(;|$);if(arr !=null)returnunescape(arr2);return

10、null11、原生JavaScript加入收藏夹1234567891011functionAddFavorite(sURL, sTitle) trywindow.external.addFavorite(sURL, sTitle)catch(e) trywindow.sidebar.addPanel(sTitle, sURL,)catch(e) alert(加入收藏失败,请使用Ctrl+D进行添加)12、原生JavaScript设为首页12345678910111213141516functionsetHomepage() if(document.all) document.body.styl

11、e.behavior =url(#default#homepage);document.body.setHomePage(http:/www.jq-)elseif(window.sidebar) if(scape) trynetscape.security.PrivilegeManager.enablePrivilege(UniversalXPConnect)catch(e) alert(该操作被浏览器拒绝,如果想启用该功能,请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true)varprefs = Components.classesmozilla.org/preferences-service;1.getService(Components.interfaces.nsIPrefBranch);prefs.setCharPref(browser.startup.homepage,http:/www.jq-)13、原生JavaScript判断IE61234567varua = navigat

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 总结/报告

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号