创建 SVG 和矩形不起作用
Create SVG and Rectangle not working
我正在尝试创建一个简单的库,以便在HTML上使用SVG绘制。虽然代码生成有效的 HTML 可以绘制我想要的内容,但当我以编程方式执行此操作时,屏幕中不会绘制任何内容。这是我正在使用的代码:
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<div id="diagram"></div>
<script type="text/javascript">
function Diagram(div) {
div = document.getElementById(div);
svg = document.createElement("svg");
svg.setAttribute("width", "600");
svg.setAttribute("height", "400");
div.appendChild(svg);
this.diagram = svg;
this.rectangle = function (x, y, width, height) {
rect = document.createElement("rect");
rect.setAttribute("x", x);
rect.setAttribute("y", y);
rect.setAttribute("width", width);
rect.setAttribute("height", height);
this.diagram.appendChild(rect);
}
}
var diagram = new Diagram("diagram");
diagram.rectangle(100, 100, 100, 100);
</script>
</body>
</html>
不用说,我对JS没有经验,我将其用作学习JS和SVG的练习,但是我无法理解为什么我的示例不起作用:-(
创建 SVG 元素时,您将使用 createElementNS
创建具有限定命名空间的元素,例如 SVG 元素
document.createElementNS("http://www.w3.org/2000/svg", "svg");
对于某些属性,您会使用 setAttributeNS
,但对于宽度和高度等常规属性,setAttribute
应该有效
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
小提琴
相关文章:
- SVG使用jQuery,高斯滤波器不起作用
- inline svg--getElementById在Opera中不起作用
- javascript中的SVG转换在IE 11中不起作用
- 在SVG中创建带有外来对象的文本区域的Javascript不起作用
- JavaScript在嵌入SVG文件中不起作用
- 对链接的 svg 进行动画处理不起作用;内联时工作正常
- <标题>插入SVG的元素不起作用
- 在svg'It’不起作用
- 使用svg.js的代码在JSFiddle.com或我的笔记本电脑中不起作用
- SVG元素的动态动画在IE中不起作用
- Svg标签在IE8中不起作用
- SVG作为标记的图标在IE中不起作用
- NVD3图表控件don'在svg元素上挂接鼠标事件时不起作用
- 单击事件在svg路径上不起作用
- 垂直反转拉斐尔 SVG 形状不起作用
- 创建 SVG 和矩形不起作用
- SVG 在安卓浏览器上不起作用.无法调试它
- SVG 圆圈脉冲同步不起作用
- d3js 加载静态 svg 在 IE9 中不起作用(responseXML = null)
- Monaca / Onsen 2.0 - 图表库在
中不起作用(SVG和Canvas)