我想用JS创建一个SVG并设置大小,但是当我使用createElement("svg")
时,生成的HTML是
<svg class="jscreated" style="width: 500px; height: 400px;"></svg>
但是svg大小显示为0,0
。
参见此示例:
var svg=document.createElement("svg");
document.body.appendChild(svg);
svg.setAttribute("class","jscreated");
svg.style.width="500px";
svg.style.height="400px";
<svg class="HTML_SVG" style="width:500px; height:400px;" class="HTML_SVG"></svg>
您可以看到,JS创建的SVG是0,0
,但是在HTML书面中直接创建的SVG应该是500x400。Chrome检查器中的"==$0"
是什么意思?
发布于 2016-10-12 03:15:34
createElement只能创建createElementNS元素,您需要createElementNS
var svg=document.createElementNS("http://www.w3.org/2000/svg", "svg");
document.body.appendChild(svg);
svg.setAttribute("class","jscreated");
svg.style.width="500px";
svg.style.height="400px";
<svg class="HTML_SVG" style="width:500px; height:400px;" class="HTML_SVG"></svg>
https://stackoverflow.com/questions/39997113
复制