jquery - Javascript-Generated SVG Not Updating - Stack Overflow

I have a script that adds elements to an inline SVG image via jQuery, but the new elements don't s

I have a script that adds elements to an inline SVG image via jQuery, but the new elements don't seem to be showing up. The output is perfectly valid; I can copy it into the original file, reload it, and it will render just fine. But when the script generates it, the new elements aren't visible.

Here is a snippet that replicates the problem:

Thanks in advance!

I have a script that adds elements to an inline SVG image via jQuery, but the new elements don't seem to be showing up. The output is perfectly valid; I can copy it into the original file, reload it, and it will render just fine. But when the script generates it, the new elements aren't visible.

Here is a snippet that replicates the problem: http://tinkerbin./7OmDWlsz

Thanks in advance!

Share Improve this question edited Jul 3, 2012 at 9:14 SpacedMonkey 2,7831 gold badge17 silver badges17 bronze badges asked Jul 3, 2012 at 2:44 user1318416user1318416 7591 gold badge6 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You will not see any svg output if the elements are not in the svg namespace.

Try replacing your script snippet with this:

var slices = 10;

for(i = 0; i < 360; i += 360 / slices) {
  var element = document.createElementNS("http://www.w3/2000/svg", "polyline");
  element.setAttribute("points", "0,0 -10,100 10,100");
  element.setAttribute("transform", "rotate(" + i + ")");
  $('#rotate').append(element);
}

Thank you to much @Erik Dahlström, I may add a contribution to this. Here is the function I define in order to build svg trees :

Node.prototype.svg_grow = function(node_name, node_attr) {
    n = document.createElementNS("http://www.w3/2000/svg", node_name);
    this.appendChild(n);
    if (typeof node_attr !== 'undefined') {
        for (key in node_attr) {
            n.setAttribute(key, node_attr[key]);
        }
    }
    return n;
}

So you can just use svg_grow() on any node like this :

s_g = document.getElementById("parent");
s_rect = s_g.svg_grow('rect', {x:0, y:0, width:480, height:640});

which just do :

<g id="parent">
    <rect x="0" y="0" width="480" height="640" />
</g>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745107176a4611629.html

相关推荐

  • jquery - Javascript-Generated SVG Not Updating - Stack Overflow

    I have a script that adds elements to an inline SVG image via jQuery, but the new elements don't s

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信