把字符串转成unicode编码
思路
1、判断字符串是否为空
2、若不为空,根据字符串长度length,循环遍历字符串,使用charCodeAt方法逐个转成unicode编码
3、返回unicode编码
方法
/*
*把给定字符串转成unicode编码
*str:待转吗字符串
*/
function toUnicode(str){
if(!str){ return; }
var codeStr = '';
for(var i=0; i<str.length; ++i){
codeStr += '\\u' + parseInt(str.charCodeAt(i),10).toString(16);
}
return codeStr;
}