Javascript - Change html tag type

后端 未结 8 1089
说谎
说谎 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);
    // "<div><span></span></div>"
    

    More information in the this answer:

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

    0 讨论(0)
  • 2020-11-30 14:52

    Use

    var href=$("#aId").attr('href');
    
    $("#aId").replaceWith($("<div"+href+">" + $("#aId").innerHTML + "</div>"));
    
    0 讨论(0)
提交回复
热议问题