namespace attributes not present in .outerHTML

前端 未结 2 1934
清歌不尽
清歌不尽 2021-01-27 05:26

In HTML5/JavaScript, if I create a namespaced dom node programmatically, like so:

svgElt = document.createElementNS(\"http://www.w3.org/2000/svg\", \'svg\')
         


        
2条回答
  •  不要未来只要你来
    2021-01-27 05:33

    You can simply use an XMLSerializer for this, which will stringify your DOM tree to xml and thus preserve the namespaces.

    const svgElt = document.createElementNS("http://www.w3.org/2000/svg", 'svg')
    const serialized = new XMLSerializer().serializeToString(svgElt);
    console.log(serialized);

提交回复
热议问题