Javascript - Change html tag type

后端 未结 8 1171
说谎
说谎 2020-11-30 14:03

i have a question, can i replace html tag to another ?

But i don\'t want to make the content blank. like this:

content<         


        
8条回答
  •  悲哀的现实
    2020-11-30 14:48

    2018 Update:

    Use ChildNode.replaceWith() method. As exemplified in MDN docs:

    var parent = document.createElement("div");
    var child = document.createElement("p");
    parent.appendChild(child);
    var span = document.createElement("span");
    
    child.replaceWith(span);
    
    console.log(parent.outerHTML);
    // "
    "

    More information in the this answer:

    Please beware that it as July 2018 is an experimental technology and not supported by IE.

提交回复
热议问题