【发布时间】:2023-03-31 18:15:02
【问题描述】:
我是SVG 的新手,所以我提前为我的无知道歉。
我创造了一个小提琴,只是在玩弄一些东西。 http://jsfiddle.net/a46p8/
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width','200');
svg.setAttribute('height','200');
var line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('width', '0');
line.setAttribute('height', '0');
line.setAttribute('x1', '0');
line.setAttribute('y1', '0');
line.setAttribute('x2', '150');
line.setAttribute('y2', '150');
line.setAttribute('stroke', 'rgb(255,0,0)');
line.setAttribute('stroke-width', '2');
svg.appendChild(line);
var ct = document.getElementById('container');
ct.appendChild(svg);
真的没有更简单的setAttributes方法吗?例如,是否有可能将它们与这样的东西结合起来:
line.setAttribute('x1', '0', 'y1', '0', 'x2', '150', 'y2', '150');
是的,我知道当我尝试小提琴时它不起作用。但是有什么方法可以做到吗?如果不是,那是什么原因你不能呢?
【问题讨论】:
-
编写自己的函数...
标签: javascript svg setattribute