开发者

createTextNode without htmlentities

开发者 https://www.devze.com 2023-03-11 11:16 出处:网络
Consider the following snippet: var t = \"<span>Hello world</span>\"; var range = window.getSelection().getRangeAt(0);

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);
0

精彩评论

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

关注公众号