开发者

escaping html markup

开发者 https://www.devze.com 2023-04-07 21:41 出处:网络
I\'m building a html table using javascript on the fly and I want to add a cell that may contain html markup.I do not want that ma开发者_运维知识库rkup interpreted but simply displayed.

I'm building a html table using javascript on the fly and I want to add a cell that may contain html markup. I do not want that ma开发者_运维知识库rkup interpreted but simply displayed.

  LogHTML += "<tr><td>" + data + "</td></tr>";

How can I do that? Is there some function or html markup that will accomplish that?

I tried pre but then I get extra spaces.


LogHTML += "<tr><td>"+data.replace(/</g,"&lt;")+"</td></tr>";

That's all you need.


The best solution is to use DOM instead of innerHTML and to create text node for data.

But you can try this as a quick solution or

function htmlentities(s){
    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消