JS - Remove a tag without deleting content

前端 未结 8 1085
渐次进展
渐次进展 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:45

    It's just three text nodes instead of one. It doesn't make a visible difference does it?

    If it's a problem, use the DOM normalize method to combine them:

    $(...)[0].normalize();
    
    0 讨论(0)
  • 2021-01-04 04:47

    There are several ways to do it. Jquery is the most easy way:

    //grab and store inner span html
    var content = $('p span').html;
    //"Re"set inner p html
    $('p').html(content);
    

    Javascript can do the same using element.replace. (I don't remember the regex to do the replace in one stroke, but this is the easy way)

    paragraphElement.replace("<span>", "");
    paragraphElement.replace("</span>", "");
    
    0 讨论(0)
提交回复
热议问题