Change node type

前端 未结 4 1616
广开言路
广开言路 2021-01-05 03:44

Using jQuery, is it possible to change all matched elements to a different type?

For example, can I select all a tags, $(\"a\"), and change

4条回答
  •  星月不相逢
    2021-01-05 04:22

    Using standard JavaScript's ChildNode.replaceWith() method can do this. Run example.

    var element = document.querySelector("span");
    var elementInnerHTML = element.innerHTML;
    var newElement = document.createElement("h1");
    newElement.innerHTML = elementInnerHTML;
    
    element.replaceWith(newElement);
    
    
    
      
      
      hightekk.com
    
    
      
        Hello World
      
    
    

提交回复
热议问题