jquery's append not working with svg element?

后端 未结 14 2185
北海茫月
北海茫月 2020-11-21 06:28

Assuming this:



 
 

        
14条回答
  •  感动是毒
    2020-11-21 06:45

    I haven't seen someone mention this method but document.createElementNS() is helpful in this instance.

    You can create the elements using vanilla Javascript as normal DOM nodes with the correct namespace and then jQuery-ify them from there. Like so:

    var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'),
        circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
    
    var $circle = $(circle).attr({ //All your attributes });
    
    $(svg).append($circle);
    

    The only down side is that you have to create each SVG element with the right namespace individually or it won't work.

提交回复
热议问题