get opening tag including attributes - outerHTML without innerHTML

后端 未结 7 1342
花落未央
花落未央 2020-12-10 03:22

I would like to retrieve a certain tag element with its attributes from the DOM. For example, from


  link text
         


        
7条回答
  •  佛祖请我去吃肉
    2020-12-10 03:32

    If someone is not using jQuery . . .

    elem.outerHTML
    "
      link text
    "
    elem.cloneNode().outerHTML
    ""
    

    If you want to be safe for Firefox etc. released before mid-2014, use cloneNode(false) to avoid getting inner stuff.

    Reference: https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode

提交回复
热议问题