Is there any major difference between innerHTML an

2019-01-23 11:35发布

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

2条回答
小情绪 Triste *
2楼-- · 2019-01-23 11:56

My understanding is that certain manipulations of innerHTML remove all bound events, so using createTextNode is preferable.

\"骚年 ilove
3楼-- · 2019-01-23 12:08

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).

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

登录 后发表回答