- 1、本文档共31页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
原生JavaScript技巧大收集
1、原生JavaScript实现字符串长度截取
function cutstr(str, len) {
??? var temp;
??? var icount = 0;
??? var patrn = /[^\x00-\xff]/;
??? var strre = ;
??? for (var i = 0; i str.length; i++) {
??????? if (icount len - 1) {
??????????? temp = str.substr(i, 1);
??????????? if (patrn.exec(temp) == null) {
??????????????? icount = icount + 1
??????????? } else {
??????????????? icount = icount + 2
??????????? }
??????????? strre += temp
??????? } else {
??????????? break
??????? }
??? }
??? return strre + ...
}
2、原生JavaScript获取域名主机
function getHost(url) {
??? var host = null;
??? if(typeof url == undefined|| null == url) {
??????? url = window.location.href;
??? }
??? var regex = /^\w+\:\/\/([^\/]*).*/;
??? var match = url.match(regex);
??? if(typeof match != undefined null != match) {
??????? host = match[1];
??? }
??? return host;
}
3、原生JavaScript清除空格
Stotype.trim = function() {
??? var reExtraSpace = /^\s*(.*?)\s+$/;
??? return this.replace(reExtraSpace, $1)
}
4、原生JavaScript替换全部
Stotype.replaceAll = function(s1, s2) {
??? return this.replace(new RegExp(s1, gm), s2)
}
5、原生JavaScript转义html标签
function HtmlEncode(text) {
??? return text.replace(//g, ).replace(/\/g, ).replace(//g, ).replace(//g, )
}
6、原生JavaScript还原html标签
function HtmlDecode(text) {
??? return text.replace(//g, ).replace(//g, \).replace(//g, ).replace(//g, )
}
7、原生JavaScript时间日期格式转换
Dtotype.Format = function(formatStr) {
??? var str = formatStr;
??? var Week = [日, 一, 二, 三, 四, 五, 六];
??? str = str.replace(/yyyy|YYYY/, this.getFullYear());
??? 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, Week[this.getDay()]);
??? str = str.replace(/dd|DD/, this.getDate() 9 ? this.getDate().toString() : 0 + this.getDate());
??? str = str.replace(/d|D/g, this.getDate(
您可能关注的文档
最近下载
- 附着式抱杆组塔施工方案(外抱杆).doc VIP
- 2025全国普通高等学校体育单招试题语文试卷.docx VIP
- 农村教师公开选调进城考试模拟试题1(初中地理·附参考答案).docx
- 电子式绝缘电阻测试仪讲解文档.ppt VIP
- 驻校教官培训课件.pptx
- GBT 21237-2018 石油天然气输送管用宽厚钢板.pdf
- 安徽泾县国有资产投资运营公司、泾县中小企业融资招聘笔试题库2025.pdf
- 安徽宣城市泾县国有资本投资运营控股集团有限公司招聘笔试题库2025.pdf
- 安徽宣城宁国市国有资产投资运营有限公司招聘笔试题库2023.pdf VIP
- 安徽芜湖市国有资本投资运营有限公司招聘笔试题库2022.pdf VIP
文档评论(0)