Encode HTML entities

自古美人都是妖i 提交于 2019-12-01 18:29:41

Basically you should encode your html entities into html as such:

var encodedStr = data['1']['result']['content'];
var a = $("#content").html(encodedStr).text();

Then get the encoded text and apply it as html() as such:

$("#content").html(a);

That should work.

Demo: http://jsbin.com/ihadam/9/edit

You can save the bloat of JQuery with pure JavaScript functions.

Sometimes you just want to encode every character... This function replaces "everything except for nothing" in regxp.

function encode(e){return e.replace(/[^]/g,function(e){return"&#"+e.charCodeAt(0)+";"})}

function encode(w) {
  return w.replace(/[^]/g, function(w) {
    return "&#" + w.charCodeAt(0) + ";";
  });
}

test.value=encode(document.body.innerHTML.trim());
<textarea id=test rows=11 cols=55>www.WHAK.com</textarea>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!