简体   繁体   English

如何使用javascript添加svg?

[英]How to add svg using javascript?

I am using this code to insert svg into a div tag. 我正在使用此代码将svg插入div标签。

var container = document.getElementById("div_id");
var svg = document.createElement("svg");
svg.setAttribute("width",container.clientWidth);
svg.setAttribute("height",container.clientHeight);    
container.appendChild(svg);

On checking using the developer tools in the browser, svg is present inside the div. 使用浏览器中的开发人员工具进行检查时,svg位于div中。 But when I hover over the svg in the developer tools, it is showing "svg 0*0" ie eventhough the width and height attributes are set as 500 and 400 I cannot view it in the page. 但是,当我将鼠标悬停在开发人员工具中的svg上时,它显示为“ svg 0 * 0”,即使将width和height属性设置为500和400,我也无法在页面中查看它。 I tried to insert a line into the svg, which again can be seen inside svg, but not visible in the browser. 我试图在svg中插入一行,这又可以在svg中看到,但在浏览器中看不到。 Need help. 需要帮忙。

Use 采用

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

to create your SVG. 创建您的SVG。 Use it for elements as well. 也将其用于元素。

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

The work just like regular elements. 工作就像常规元素一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM