Consider the following snippet:
var t = "<span>Hello world</span>";
var range = window.getSelection().getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(t));
开发者_开发百科
How can I avoid replacing t
with htmlentities?
By not creating a textnode. Instead use range.pasteHTML.
Don't use a textnode.
var t = document.createElement('span');
t.textContent = 'Hello world';
var range = window.getSelection().getRangeAt(0);
range.deleteContents();
range.insertNode(t);
精彩评论