Programmatically creating an SVG image element with javascript

后端 未结 2 1841
眼角桃花
眼角桃花 2020-12-02 18:42

Like my title says, I\'m trying to programmatically creating an SVG image element in a HTML page using javascript. For some reason my basic javascript code isn\'t working, h

2条回答
  •  忘掉有多难
    2020-12-02 19:09

    SVG native attributes (not including xlink:href) do not share the SVG namespace; you can either use just setAttribute instead of setAttributeNS, or use

    svgimg.setAttributeNS(null,'x','0');
    

    for example.

    Here it is, working: http://jsfiddle.net/UVFBj/8/

    Note that I changed your fiddle to properly use XHTML, so that SVG works nicely within it in all major browsers.

提交回复
热议问题