js将中文转换为unicode编码
var columns = [{ "data": "name", "title": "张三" }, { "data": "sex", "title": "男" }, { "data": "class", "title": "二年三班" }] function toUnicode (arr){ var strArr=[]; for(var i = 0;i<arr.length;i++){ strArr.push(arr[i].title); } var str = strArr.join(','); function isChinese(s){ return /[\u4e00-\u9fa5]/.test(s); } function ch2Unicdoe(str){ if(!str){ return; } var unicode = ''; for (var i = 0; i < str.length; i++) { var temp = str.charAt(i); if(isChinese(temp)){ unicode += '\\u' + temp.charCodeAt(0).toString(16); } else{ unicode += temp; } } return unicode; } var aa = ch2Unicdoe(str); var bb = aa.split(',') for(var i = 0;i<arr.length;i++){ for(var j=0;j<bb.length;j++){ arr[i].title = bb[i]; } } return arr; }
调用这个方法之后输出的张三,男,三年二班就已转成unicode编码格式