jquery's append not working with svg element?

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

Assuming this:



 
 

        
相关标签:
14条回答
  • 2020-11-21 07:03

    Based on @chris-dolphin 's answer but using helper function:

    // Creates svg element, returned as jQuery object
    function $s(elem) {
      return $(document.createElementNS('http://www.w3.org/2000/svg', elem));
    }
    
    var $svg = $s("svg");
    var $circle = $s("circle").attr({...});
    $svg.append($circle);
    
    0 讨论(0)
  • 2020-11-21 07:06

    I would suggest it might be better to use ajax and load the svg element from another page.

    $('.container').load(href + ' .svg_element');
    

    Where href is the location of the page with the svg. This way you can avoid any jittery effects that might occur from replacing the html content. Also, don't forget to unwrap the svg after it's loaded:

    $('.svg_element').unwrap();
    
    0 讨论(0)
提交回复
热议问题