宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应

[JS]特殊字符转义源代码说明

2016-10-13 00:16:48 点击量:56 标签: 收藏本文

[JS]特殊字符转义源代码说明

  1. /*特殊字符转义*/ 
  2. function htmlspecialchars (str) { 
  3.     var strstr = str.toString().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, '&quot;'); 
  4.     return str; 
  5. } 
  6. /* 
  7.  *时间格式化 
  8.  *例子:time = new Date().Format("yyyy-MM-dd hh:mm:ss"); 
  9.  */ 
  10. Date.prototype.Format = function (fmt) {  
  11.     var o = { 
  12.         "M+": this.getMonth() + 1, //月份  
  13.         "d+": this.getDate(), //日  
  14.         "h+": this.getHours(), //小时  
  15.         "m+": this.getMinutes(), //分  
  16.         "s+": this.getSeconds(), //秒  
  17.         "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
  18.         "S": this.getMilliseconds() //毫秒  
  19.     }; 
  20.     if (/(y+)/.test(fmt)){ 
  21.         fmtfmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 
  22.     }  
  23.     for (var k in o){ 
  24.         if (new RegExp("(" + k + ")").test(fmt)){ 
  25.             fmtfmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); 
  26.         } 
  27.     }      
  28.     return fmt; 
  29. } 
  30. /* 
  31.  *获取URL参数 
  32.  */ 
  33. function getQueryString(key){ 
  34.     var reg = new RegExp("(^|&)"+ key +"=([^&]*)(&|$)"); 
  35.     var r = window.location.search.substr(1).match(reg); 
  36.     if(r!=null){ 
  37.         return  unescape(r[2]); 
  38.     } 
  39.     return null; 
  40. }