我正在将纯 JS 文本高亮脚本转换为 jQuery,但我试图弄清楚 jQuery 等价于什么insertNode
:
var selection = window.getSelection().getRangeAt(0);
var selectedText = selection.extractContents();
var span = document.createElement("span");
span.style.backgroundColor = "yellow";
span.appendChild(selectedText);
selection.insertNode(span);
我想我可以使用以下内容:
var span = $("<span class='highlight'>" + selectedText + "</span>");
selection.insertNode(span);
结果是:
NOT_FOUND_ERR: DOM Exception 8: An attempt was made to reference a Node in a context where it does not exist.