开发者

How to copy and paste SVG node using jQuery

开发者 https://www.devze.com 2023-04-04 01:29 出处:网络
I have some SVG embedded into a web page and need to copy a particular SVG node (including it\'s children), then paste it back into the DOM. Only problem is, the SVG node doesn\'t appear after being p

I have some SVG embedded into a web page and need to copy a particular SVG node (including it's children), then paste it back into the DOM. Only problem is, the SVG node doesn't appear after being pasted, which is probably because i开发者_如何学JAVAt's namespaced.

So, how can I copy and paste a namespaced SVG node? I imagine it'll be some sort of recursive function.

P.S. currently using jQuery's clone() method to copy the SVG node.

Test SVG:

<g>

    <rect>
    <text></text>

    <g>

        <circle>
        <rect>

    </g>

</g>

How do I recursively add that SVG to the DOM, remembering that createElementNS should be used because SVG is namespaced.


Seems like the solution can be acheived by using jQuery's $.parseXML function.

I thank you.


Maybe recursively create a copy and create elements using createElementNS

Like

$(mySvg).append(document.createElementNS("http://www.w3.org/2000/svg", "circle")); 

(from Is it possible to work with jquery and Svg directly (no plugins)?)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号