问题描述
标题很清楚:innerHTML
和createTextNode
(与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中.如果您不希望这样做(除非您确定文本不包含未转义的标签,例如,直接分配文字时),则可以使用(或(对于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填充跨度之间有什么主要区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!