jjzjj

surroundContents

全部标签

javascript - 你如何在 javascript 中撤消 "surroundContents"?

我正在编写一个脚本,需要在页面上四处移动环绕节点元素。我发现当我这样做时,我删除了之前包裹的child。如何取消嵌套节点的子节点,以便我可以将该父节点移动到其他地方?我是这样想的:varparg=document.getElementById("blah");if(parg.hasChildNodes()){varchildren=parg.childNodes;while(children.length>0){parg.insertBefore(parg.firstChild);parg.removeChild(parg.firstChild);};};我猜测问题所在的那一行是“in

javascript - JS - surroundContents 仅在文本上保留约 20% 的高亮尝试

我正在使用mouseup事件来触发一个函数,该函数突出显示文本并用跨度包围突出显示的文本(来自堆栈溢出的函数):functionhighlightText(e){vart=window.getSelection().toString();if(t){$("#mySpan").remove();varrange=window.getSelection().getRangeAt(0);newNode=document.createElement("span");newNode.id='mySpan';range.surroundContents(newNode);}}我遇到的主要问题是,只

javascript - 跨多个标签的 getSelection 和 surroundContents

我有一个脚本可以更改所选文本的背景颜色。但是,当跨多个元素/标签选择文本时,我遇到了一个问题。我得到的代码是:vartext=window.getSelection().getRangeAt(0);varcolour=document.createElement("hlight");colour.style.backgroundColor="Yellow";text.surroundContents(colour);输出的错误是:Error:Theboundary-pointsofarangedoesnotmeetspecificrequirements.=NS_ERROR_DOM_R