如何让Chrome重绘SVG动态添加内容?

Mes*_*esh 25 javascript svg

我已经将圈元素添加到iFrame中显示的svg中.Chrome没有显示新元素,尚未尝试FF.我需要调用一些重绘/刷新吗?第一个圆圈实际上是在svg文档中,其余的来自脚本.

<iframe id="svgFrame" src="xmlfile1.svg" width="300" height="300">
<svg xmlns="http://www.w3.org/2000/svg" id="SVG1" width="200" height="200">
   <circle cx="20" cy="20" r="5"/>
   <circle cx="165" cy="80" r="32"/>
   <circle cx="15" cy="38" r="32"/>
   <circle cx="140" cy="39" r="30"/>
   <circle cx="178" cy="32" r="22"/>
   ...etc
   <circle cx="166" cy="130" r="16"/>
</svg>
</iframe>
Run Code Online (Sandbox Code Playgroud)

创建元素的javascript:

function RandomNumber(min, max) {
    var r;
    r = Math.floor(Math.random() * (max - min + 1)) + min;
    return r;
}

var svg = document.getElementById("svgFrame").contentDocument;

for (var i = 0; i < 99; i++) {

    var n = svg.createElement("circle");
    n.setAttribute("cx" , RandomNumber( 0 , 200) );
    n.setAttribute("cy" , RandomNumber(0, 200) );
    n.setAttribute("r"  , RandomNumber(5, 35) );

    svg.documentElement.appendChild(n);
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*nau 20

我没有尝试过你在做什么,你基本上有两个来源,但我知道Chrome在动态添加内容时不需要刷新/重绘.

这是我的代码,也许这会对你有所帮助.

xmlns = "http://www.w3.org/2000/svg";
var C = document.createElementNS(xmlns,"circle");
C.setAttributeNS(null, "cx", cx);
C.setAttributeNS(null, "cy", cy);
C.setAttributeNS(null, "r", rad);
document.getElementById("background").appendChild(C);
Run Code Online (Sandbox Code Playgroud)

背景只是一个组的id(g标签)


Mat*_*son 14

我猜,但你试过createElementNS("http://www.w3.org/2000/svg","circle")而不是createElement("circle")