金沙贵宾会官网|金沙贵宾会登录-官网

【A】金沙贵宾会官网超高的返奖率为娱乐者提供资金保障,所以金沙贵宾会登录官网更加的方便了你的娱乐,申请88元彩金,因为在当中不仅仅只有游戏。

11个原生JavaScript技艺分享,JavaScript的拾二个原生

日期:2019-10-04编辑作者:网络软件

原文地址:

1、实现字符串长度截取

JavaScript的10个原生技巧分享

   本文给大家分享的是个人总结的10条非常常用的原生javascript的小技巧,都是平时项目中用到的,这里总结记录下来,有需要的小伙伴可以参考下。

  1、实现字符串长度截取

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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、获取域名主机

  ?

1

2

3

4

5

6

7

8

9

10

11

12

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、清除空格

  ?

1

2

3

4

String.prototype.trim = function() {

var reExtraSpace = /^s*(.*?)s+$/;

return this.replace(reExtraSpace, "$1")

}

  4、替换全部

  ?

1

2

3

String.prototype.replaceAll = function(s1, s2) {

return this.replace(new RegExp(s1, "gm"), s2)

}

  5、转义html标签

  ?

1

2

3

function HtmlEncode(text) {

return text.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>')

}

  6、还原html标签

  复制代码 代码如下:

  function HtmlDecode(text) {

  return text.replace(/&/g, '&').replace(/"/g, '"').replace(//g, '>')

  }

  7、时间日期格式转换

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Date.prototype.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());

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/, this.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());

return str

}

  8、判断是否为数字类型

  ?

1

2

3

4

5

6

7

8

function isDigit(value) {

var patrn = /^[0-9]*$/;

if (patrn.exec(value) == null || value == "") {

return false

} else {

return true

}

}

  9、设置cookie值

  ?

1

2

3

4

5

6

7

8

9

function setCookie(name, value, Hours) {

var d = new Date();

var offset = 8;

var utc = d.getTime() + (d.getTimezoneOffset() * 60000);

var nd = utc + (3600000 * offset);

var exp = new Date(nd);

exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);

document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;"

}

  10、获取cookie值

  ?

1

2

3

4

function getCookie(name) {

var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));

if (arr != null) return unescape(arr[2]);

return null

  以上所述就是本文的全部内容了,希望大家能够喜欢。

本文给大家分享的是个人总结的10条非常常用的原生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 + "..."
 }

图片 1

2、获取域名主机

1、原生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;
 }
function cutstr { 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; if (patrn.exec == null) { icount = icount + 1 } else { icount = icount + 2 } strre += temp } else { break } } return strre + "..."}

3、清除空格

2、原生JavaScript获取域名主机

 String.prototype.trim = function() {
   var reExtraSpace = /^s*(.*?)s+$/;
   return this.replace(reExtraSpace, "$1")
 }
function getHost { var host = "null"; if(typeof url == "undefined"|| null == url) { url = window.location.href; } var regex = /^w+://.*/; var match = url.match; if(typeof match != "undefined" && null != match) { host = match[1]; } return host;}

4、替换全部

3、原生JavaScript清除空格

 String.prototype.replaceAll = function(s1, s2) {
   return this.replace(new RegExp(s1, "gm"), s2)
 }
String.prototype.trim = function() { var reExtraSpace = /^s*s+$/; return this.replace(reExtraSpace, "$1")}

5、转义html标签

4、原生JavaScript替换全部

 function HtmlEncode(text) {
   return text.replace(/&/g, '&amp').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>')
 }
String.prototype.replaceAll = function { return this.replace(new RegExp, s2)}

6、还原html标签

5、原生JavaScript转义html标签

复制代码 代码如下:

function HtmlEncode { return text.replace(/&/g, '&amp').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>')}

 function HtmlDecode(text) {
     return text.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>')
 }

6、原生JavaScript还原html标签

7、时间日期格式转换

function HtmlDecode { return text.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>')}
 Date.prototype.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());
   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/, this.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());
   return str
 }

7、原生JavaScript时间日期格式转换

8、判断是否为数字类型

Date.prototype.Format = function(formatStr) { var str = formatStr; var Week = ['日', '一', '二', '三', '四', '五', '六']; str = str.replace(/yyyy|YYYY/, this.getFullYear; str = str.replace(/yy|YY/, (this.getYear > 9 ? (this.getYear.toString() : '0' + (this.getYear; str = str.replace(/MM/, (this.getMonth > 9 ? (this.getMonth.toString() : '0' + (this.getMonth; str = str.replace(/M/g, (this.getMonth; 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; 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/, this.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; return str}
 function isDigit(value) {
   var patrn = /^[0-9]*$/;
   if (patrn.exec(value) == null || value == "") {
     return false
   } else {
     return true
   }
 }

8、原生JavaScript判断是否为数字类型

9、设置cookie值

function isDigit { var patrn = /^[0-9]*$/; if (patrn.exec == null || value == "") { return false } else { return true }}
 function setCookie(name, value, Hours) {
   var d = new Date();
   var offset = 8;
   var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
   var nd = utc + (3600000 * offset);
   var exp = new Date(nd);
   exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);
   document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;"
 }

9、原生JavaScript设置cookie值

10、获取cookie值

function setCookie(name, value, Hours) { var d = new Date(); var offset = 8; var utc = d.getTime() + (d.getTimezoneOffset() * 60000); var nd = utc + (3600000 * offset); var exp = new Date; exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); document.cookie = name + "=" + escape + ";path=/;expires=" + exp.toGMTString() + ";domain=x.com;"}
 function getCookie(name) {
   var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
   if (arr != null) return unescape(arr[2]);
   return null

10、原生JavaScript获取cookie值

以上所述就是本文的全部内容了,希望大家能够喜欢。

function getCookie { var arr = document.cookie.match(new RegExp" + name + "=; if (arr != null) return unescape; return null}

您可能感兴趣的文章:

  • 你有必要知道的10个JavaScript难点
  • Web性能优化系列 10个提升JavaScript性能的技巧
  • 极力推荐10个短小实用的JavaScript代码段
  • 分享10个优化代码的CSS和JavaScript工具
  • 10个JavaScript中易犯小错误
  • JavaScript开发人员的10个关键习惯小结
  • 10个在JavaScript开发中常遇到的BUG

本文由金沙贵宾会官网发布于网络软件,转载请注明出处:11个原生JavaScript技艺分享,JavaScript的拾二个原生

关键词:

一波神奇的Python语句,Python奇技淫巧

当公布python第三方package时,并不指望代码中有着的函数或然class能够被外表import,在 __init__.py 中添加 __all__ 质量,该...

详细>>

MSYS2开发环境搭建,编译Windows版智能坊钱包

后天都以容器技艺,用 vagrant 恐怕 docker 布贰个 linux 的镜像,安装些linux 的工具链,比方 g++ php ngingx等等,分享给别...

详细>>

AlphaGo再次胜出,日经中文网

阿尔法狗的求学本领很强,第二盘的作战中黑狗略有失误,但黑狗是有强有力的深浅神经网络练习的,每趟对局也是...

详细>>

一个很有借鉴价值的编程故事,一个理智的生命

摘要: 七日七日,每日学一些在线课程,哪怕二回独有半个小时,只要坚韧不拔,你就会感到到温馨前日做出的决定...

详细>>