remove wrapping div and leave all sub-divs intact?

前端 未结 4 1330
青春惊慌失措
青春惊慌失措 2020-12-01 09:35

I have one wrapper div with several sub-divs inside and tags inside those sub-divs as well. I want to remove the wrapper div. I have considered JQuery\'s unwrap, but it ap

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 10:01

    function unwrap(el){
        var parent = el.parentNode; // get the element's parent node
        while (el.firstChild){
            parent.insertBefore(el.firstChild, el); // move all children out of the element
        }
        parent.removeChild(el); // remove the empty element
    }
    

    The code is straight forward and much faster than the corresponding jQuery's method $.unwrap().

    Source: https://plainjs.com/javascript/manipulation/unwrap-a-dom-element-35/

提交回复
热议问题