Javascript - Change html tag type

后端 未结 8 1149
说谎
说谎 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条回答
  •  Happy的楠姐
    2020-11-30 14:39

    Here's a way to change the tag of an element with no jQuery -

    // Just provide the element and the new tag
    
    function changeTag(el, newTag) {
        var outerHTML = el.outerHTML // Get the outerHTML of the element
        var tag = el.tagName // Get the tag of the outerHTML
        var r = new RegExp(tag, 'i') // Prepare a RegExp to replace the old tag with the new tag
        outerHTML = outerHTML.replace(r, newTag) // Replace it
        el.outerHTML = outerHTML // Set the outerHTML of the element
    }
    Span 1 -- Span 2
    

提交回复
热议问题