How can I remove wrapper (parent element) without removing the child?

后端 未结 7 1254
天命终不由人
天命终不由人 2020-12-03 20:48

I would like to remove the parent without removing the child - is this possible?

HTML structure:

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 21:35

    Pure javascript solution, i'm sure someone can simplify it more but this is an alternative for pure javascript guys.

    HTML

    Remove wrapper

    Javascript (pure)

    function unwrap(i) {
        var wrapper = i.parentNode.getElementsByClassName('wrapper')[0];
        // return if wrapper already been unwrapped
        if (typeof wrapper === 'undefined') return false;
        // remmove the wrapper from img
        i.parentNode.innerHTML = wrapper.innerHTML + i.outerHTML;
        return true;
    }
    

    JSFIDDLE

提交回复
热议问题