set img.src to dynamic svg element

前端 未结 2 694
一向
一向 2021-02-03 12:27

I know that I can set a svg file as the src of an HTML img element like this:


but can

2条回答
  •  爱一瞬间的悲伤
    2021-02-03 13:05

    This updates the accepted answer from Phrogz (8 years after it!)

    The sample does not work on Chrome or Firefox (the image appears broken) changing to

      img.src = "data:image/svg+xml;base64,"+btoa(xml);
    

    I am not sure of the reason why the original stopped working but this may help someone landing here.

提交回复
热议问题