createElement("svg")的大小为0,0 [英] size of createElement("svg") is 0,0

查看:121
本文介绍了createElement("svg")的大小为0,0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JS创建SVG并设置大小,但是当我使用createElement("svg")时,生成的HTML是

I want to create an SVG with JS and set the size, but when I use createElement("svg"), the generated HTML is

<svg class="jscreated" style="width: 500px; height: 400px;"></svg>

,但svg大小显示为0,0.

but the svg size is shown as 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"是什么意思?

You can see that the SVG created by JS is 0,0 but the direct in HTML written one is 500x400 as it should be. What does the "==$0" in the Chrome inspector mean?

推荐答案

createElement只能创建HTML元素,需要createElementNS

createElement can only create HTML elements, you need 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>

这篇关于createElement("svg")的大小为0,0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆