Can not add image inside SVG via jQuery: tag becomes

前端 未结 2 1866
独厮守ぢ
独厮守ぢ 2020-12-29 10:18

I want to add items in an SVG via jQuery. I can well insert tags such as or but I can not insert images with the tag

相关标签:
2条回答
  • 2020-12-29 10:53

    you should use createElementNS():

    var img = document.createElementNS('http://www.w3.org/2000/svg','image');
    img.setAttributeNS(null,'height','536');
    img.setAttributeNS(null,'width','536');
    img.setAttributeNS('http://www.w3.org/1999/xlink','href','https://upload.wikimedia.org/wikipedia/commons/2/22/SVG_Simple_Logo.svg');
    img.setAttributeNS(null,'x','10');
    img.setAttributeNS(null,'y','10');
    img.setAttributeNS(null, 'visibility', 'visible');
    $('svg').append(img);
    
    0 讨论(0)
  • 2020-12-29 11:14

    jQuery alone cannot handle SVG manipulation properly, as this answer explains. You should probably be using Raphael, or one of the hacky methods described in the link above.

    0 讨论(0)
提交回复
热议问题