Remove tag around a text node using javascript

前端 未结 8 1919
夕颜
夕颜 2021-02-06 14:12

If I have some HTML that looks like this:

This is some text that is being written with a high
相关标签:
8条回答
  • 2021-02-06 15:06

    This will do what you want, and also preserve any tags within the .highlight span.

    content = $(".highlight").contents();
    $(".highlight").replaceWith(content);
    
    0 讨论(0)
  • 2021-02-06 15:06

    it would be much easier to just change the class of the span than to actually remove it. You can use pure javascript:

    document.getElementById("span id").className="";
    

    Or jquery's toggleClass function:

     $("element").toggleClass("highlight");
    

    Also, best practices say that you shouldn't use class names that imply a style, like highlight. Try "emphasized" instead. :D

    0 讨论(0)
提交回复
热议问题