获取字符串长度
function GetStringLength(str) { return str.replace(/[^x00-xff]/g, "00").length; }
通过js对html转义和反转义
function HTMLEncode(html) { var temp = document.createElement("div"); (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html); var output = temp.innerHTML; temp = null; return output; } function HTMLDecode(text) { var temp = document.createElement("div"); temp.innerHTML = text; var output = temp.innerText || temp.textContent; temp = null; return output; }
一些公用的js
1 var com = com || {}; 2 (function ($, com) { 3 /** 4 * 截取字符串 5 * @param str:要截取的字符串 6 * @param len:保留多少字符 7 * @param symbol:超过之后字符串末端要添加的字符 8 */ 9 com.cutStr = function (str, len, symbol) { 10 str = str || ""; 11 if (symbol == null || symbol == undefined) 12 symbol = "..."; 13 14 var count = 0; 15 var strTemp = ""; 16 for (var i = 0; i < str.length; i++) { 17 if (/[^x00-xff]/g.test(str.substr(i, 1))) { 18 count += 2; 19 } 20 else { 21 count += 1; 22 } 23 if (count <= len) { 24 strTemp += str.substr(i, 1); 25 } 26 else { 27 return strTemp + symbol; 28 } 29 } 30 return str; 31 }, 32 /* 33 * 将日期字符串转化为Date 34 * (如:将"2016-12-24 20:13:14"转化为Date格式) 35 * @param d:待转化字符串(传入的时间不能有毫秒) 36 */ 37 com.getDate = function (d) { 38 //部分浏览器(IE)不支持日期格式“yyyy-MM-dd hh:mm:ss”,必须将“-”转化为“/” 39 var date = new Date(Date.parse(d.replace(/-/g, "/"))); 40 return date; 41 }, 42 /* 43 * 获取html代码的纯文本 44 * @param html 45 */ 46 com.removeHTMLTag = function (html) { 47 html = html.replace(/</?[^>]*>/g, ''); //去除HTML tag 48 html = html.replace(/[ | ]* /g, ' '); //去除行尾空白 49 //html = html.replace(/ [s| | ]* /g,' '); //去除多余空行 50 html = html.replace(/ /ig, '');//去掉 51 html = html.replace(/s/g, ''); //将空格去掉 52 return html; 53 }, 54 /* 55 * 获取地址栏参数 56 * @param 参数名 57 */ 58 com.getQueryString = function (name) { 59 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); 60 var r = window.location.search.substr(1).match(reg); 61 if (r != null) return (r[2]); return null; 62 }, 63 64 /************************************************************** 65 *将金额转化为大写 66 * str:需要转化的金额 67 ****************************************************************/ 68 com.convertMoney = function (n) { 69 var fraction = ['角', '分', '毫', '厘']; 70 var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; 71 var unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']]; 72 var cnInteger = "整"; //整数金额时后面跟的字符 73 var cnIntLast = "元"; //整型完以后的单位 74 var maxNum = 9999999999999.9999; 75 var head = n < 0 ? '负' : ''; 76 //取绝对值 77 n = Math.abs(n); 78 if (n >= maxNum) { 79 console.error("金额转化超出最大处理数字"); 80 return ""; 81 } 82 83 var IntegerNum; //金额整数部分 84 var DecimalNum; //金额小数部分 85 var money = n.toString(); 86 if (money.indexOf(".") == -1) { 87 IntegerNum = money; 88 DecimalNum = ''; 89 } else { 90 var parts = money.split("."); 91 IntegerNum = parts[0]; 92 DecimalNum = parts[1].substr(0, 4); 93 } 94 95 var ChineseStr = ""; 96 97 //小数部分 98 if (DecimalNum != '') { 99 for (i = 0; i < DecimalNum.length; i++) { 100 var m = DecimalNum.substr(i, 1); 101 if (m != '0') { 102 ChineseStr += digit[Number(m)] + fraction[i]; 103 } 104 } 105 } 106 107 IntegerNum = IntegerNum * 1; 108 109 //整数部分 110 for (var i = 0; i < unit[0].length && IntegerNum > 0; i++) { 111 var p = ''; 112 for (var j = 0; j < unit[1].length && IntegerNum > 0; j++) { 113 p = digit[IntegerNum % 10] + unit[1][j] + p; 114 IntegerNum = Math.floor(IntegerNum / 10); 115 } 116 ChineseStr = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + ChineseStr; 117 } 118 119 if (ChineseStr == '') { 120 ChineseStr += digit[0] + cnIntLast + cnInteger; 121 } else if (DecimalNum == '') { 122 ChineseStr += cnInteger; 123 } 124 ChineseStr = head + ChineseStr; 125 return ChineseStr; 126 }, 127 128 /** 129 * 获取字符串长度(一个汉字占两个字符) 130 * @param str:字符串 131 */ 132 com.getStringLength = function (str) { 133 return str.replace(/[^x00-xff]/g, "00").length; 134 } 135 136 })(jQuery, com);