html unicode 在线转换,将html标签转换unicode码显示

var htmlEncode = function(str){

if(!str || $.type(str) != 'string') return '';

var hex = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');

var preescape = str;

var escaped = "";

for(var i = 0; i < preescape.length; i++){

var p = preescape.charAt(i);

escaped = escaped + escapeCharx(p);

}

return escaped;

function escapeCharx(original){

var found=true;

var thechar=original.charCodeAt(0);

switch(thechar) {

case 10: return "
"; break;//newline

case 32: return " "; break;//space

case 34:return """; break;//"

case 38:return "&"; break;//&

case 39:return "'"; break;//'

case 47:return "/"; break;// /

case 60:return "<"; break;//<

case 62:return ">"; break;//>

case 198:return "Æ"; break;

case 193:return "Á"; break;

case 194:return "Â"; break;

case 192:return "À"; break;

case 197:return "Å"; break;

case 195:return "Ã"; break;

case 196:return "Ä"; break;

case 199:return "Ç"; break;

case 208:return "Ð"; break;

case 201:return "É"; break;

case 202:return "Ê"; break;

case 200:return "È"; break;

case 203:return "Ë"; break;

case 205:return "Í"; break;

case 206:return "Î"; break;

case 204:return "Ì"; break;

case 207:return "Ï"; break;

case 209:return "Ñ"; break;

case 211:return "Ó"; break;

case 212:return "Ô"; break;

case 210:return "Ò"; break;

case 216:return "Ø"; break;

case 213:return "Õ"; break;

case 214:return "Ö"; break;

case 222:return "Þ"; break;

case 218:return "Ú"; break;

case 219:return "Û"; break;

case 217:return "Ù"; break;

case 220:return "Ü"; break;

case 221:return "Ý"; break;

case 225:return "á"; break;

case 226:return "â"; break;

case 230:return "æ"; break;

case 224:return "à"; break;

case 229:return "å"; break;

case 227:return "ã"; break;

case 228:return "ä"; break;

case 231:return "ç"; break;

case 233:return "é"; break;

case 234:return "ê"; break;

case 232:return "è"; break;

case 240:return "ð"; break;

case 235:return "ë"; break;

case 237:return "í"; break;

case 238:return "î"; break;

case 236:return "ì"; break;

case 239:return "ï"; break;

case 241:return "ñ"; break;

case 243:return "ó"; break;

case 244:return "ô"; break;

case 242:return "ò"; break;

case 248:return "ø"; break;

case 245:return "õ"; break;

case 246:return "ö"; break;

case 223:return "ß"; break;

case 254:return "þ"; break;

case 250:return "ú"; break;

case 251:return "û"; break;

case 249:return "ù"; break;

case 252:return "ü"; break;

case 253:return "ý"; break;

case 255:return "ÿ"; break;

case 162:return "¢"; break;

case '\r': break;

default:

found=false;

break;

}

if(!found){

if(thechar>127) {

var c=thechar;

var a4=c%16;

c=Math.floor(c/16);

var a3=c%16;

c=Math.floor(c/16);

var a2=c%16;

c=Math.floor(c/16);

var a1=c%16;

return ""+hex[a1]+hex[a2]+hex[a3]+hex[a4]+";";

}else return original;

}

}

}

测试

htmlEncode("")

"<span>"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值