svg大小不适用于javascript [英] svg size isn't working with javascript

查看:81
本文介绍了svg大小不适用于javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

svg大小无法与javascript一起使用,我该怎么做.我尝试了所有东西,但是大小都是0px * 0px.如果我在html代码中做到这一点,则效果很好.有任何已知的原因为什么它可能行不通.如果您需要我的一些代码,请询问.

svg size isn't working with javascript no mater what I do. I tried every thing but the size is all ways 0px * 0px. If I do it in the html code it works fine. Is there any know reason why it might not work. If you need some of my code just ask.

好的,这是一些代码...

okay here's some code...

var svg = document.createElement("svg");
svg.setAttribute("style", "border: 1px solid black");
svg.setAttribute("height",20);
svg.setAttribute("width",20);
document.getElementById("container").appendChild(svg);

我还尝试了稍微不同的代码.另外给它一个边框会使其宽度和高度较小(而不是正确的宽度)

I also tried slightly different code. Also giving it a border gives it a small width and height(not the right one)

推荐答案

您不能使用createElement创建SVG元素,createElement仅适用于html元素.您必须将createElementNS与SVG命名空间结合使用,即

You can't create SVG elements with createElement, createElement is for html elements only. You must use createElementNS with the SVG namespace i.e.

var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");

这篇关于svg大小不适用于javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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