innerHTML和使用createTextNode填充跨度之间有什么主要区别吗? [英] Is there any major difference between innerHTML and using createTextNode to fill a span?

查看:62
本文介绍了innerHTML和使用createTextNode填充跨度之间有什么主要区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题很清楚: innerHTMLcreateTextNode(与Append一起使用)之间的填充文本范围有什么主要区别吗?

The title is pretty clear: Is there any major difference between innerHTML and createTextNode (used with Append) to fill a span with text?

推荐答案

当然可以. createTextNode 将转义任何字符串并按原样显示它们,而 innerHTML 可以将类似html的字符串呈现到DOM中.如果您不希望这样做(除非您确定文本不包含未转义的标签,例如,直接分配文字时),则可以使用 innerText (对于IE).

Of course. createTextNode will escape any strings and show them as they are, while innerHTML could render html-like strings into a DOM. If you don't want that (unless you are sure the text contains no unescaped tags, e.g. when assigning a literal directly), you can use textContent (or innerText for IE).

但是我还是建议您使用createTextNode,因为所有浏览器都同样支持它,没有任何怪癖.

Yet I'd recommend createTextNode, because all browsers support it equally without any quirks.

这篇关于innerHTML和使用createTextNode填充跨度之间有什么主要区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆