JS - Remove a tag without deleting content

前端 未结 8 1087
渐次进展
渐次进展 2021-01-04 04:09

I am wondering if it is possible to remove a tag but leave the content in tact? For example, is it possible to remove the SPAN tag but leave SPAN\'s content there?



        
8条回答
  •  孤城傲影
    2021-01-04 04:39

    If someone is still looking for that, the complete solution that has worked for me is:

    Assuming we have:

    hello this is the text to unwrap

    the js is:

    // get the parent
    var parentElem = $(".highlight").parent(); 
    
    // replacing with the same contents
    $(".highlight").replaceWith( 
        function() { 
            return $(this).contents();
        }
    );
    
    // normalize parent to strip extra text nodes
    parentElem.each(function(element,index){
        $(this)[0].normalize();
    });
    

提交回复
热议问题