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

Mat*_*rym 6 javascript removechild

我正在编写一个需要在页面上移动包装节点元素的脚本.我发现当我这样做时,我会移除以前包裹的孩子.如何取消节点的子节点,以便将父节点移动到其他位置?

我在想这样的事情:

  var parg = document.getElementById("blah");

  if (parg.hasChildNodes())
   {
     var children = parg.childNodes;
     while (children.length > 0)
     {
      parg.insertBefore(parg.firstChild);
      parg.removeChild(parg.firstChild);
     };
   };
Run Code Online (Sandbox Code Playgroud)

我猜测的问题是"insertBefore"逻辑.

ken*_*bec 8

insertBefore在一个元素节点上运行,并接受两个参数,即新节点和新节点将在其前面的节点.

function unwrap(who){
 var pa= who.parentNode;
 while(who.firstChild){
  pa.insertBefore(who.firstChild, who);
 }
}
Run Code Online (Sandbox Code Playgroud)

//测试

解包(的document.getElementById( "嗒嗒"));

在此输入图像描述