How did you cancel "surroundContents" in javascript?

I am writing a script that should move the node wrapping element on the page. I find that when I do this, I delete the previously wrapped children. How to disable node children so that I can move the node parent to another location?

I thought something like this:

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

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

The line I assume is a problem of the "insertBefore" logic.

+5
source share
2 answers

insertBefore works with a node element and takes two arguments, a new node, and node will be preceded by a new node.

function unwrap(who){
 var pa= who.parentNode;
 while(who.firstChild){
  pa.insertBefore(who.firstChild, who);
 }
}

//test

expand (document.getElementById ("blah"));

enter image description here

+7
source

"wrapper".

- , :

if ( parg.hasChildNodes() ) {
     var children = parg.childNodes;

     for ( child in children ) {
        child.parentNode = parg.parentNode;
     }
}
0

All Articles