简体   繁体   English

使用JavaScript和SVG创建多个节点

[英]Create multiple nodes with JavaScript and SVG

Is it possible to create multiple nodes? 是否可以创建多个节点? I have this: 我有这个:

var = document.getElementById('svgID');
var ellipse = document.createElementNS("http://www.w3.org/2000/svg", 'ellipse'); 
test.setAttribute('cx', x);
test.setAttribute('cy', y);
test.setAttribute('rx', w);
test.setAttribute('ry', h);
test.appendChild(ellipse);

Well, I can duplicate the code above, rename it and create so multiple nodes, but is there an easier way to do this? 好吧,我可以复制上面的代码,重命名并创建多个节点,但是有没有更简单的方法呢?

Easier like 更容易喜欢

addEllipse('svgID', {cx:x, cy:y, rx:w, ry:h}, test);


function addEllipse(svgID, info, toDom) {
  var = document.getElementById(svgID);
  var ellipse = document.createElementNS("http://www.w3.org/2000/svg", 'ellipse'); 
  toDom.setAttribute('cx', info.cx);
  toDom.setAttribute('cy', info.cy);
  toDom.setAttribute('rx', info.rx);
  toDom.setAttribute('ry', info.ry);
  toDom.appendChild(ellipse);
}

Thanks to all. 谢谢大家。

My problem was that my data structure was object-oriented and it was not possible to add the SVG objects to the classes because the SVG objects are added to the DOM. 我的问题是我的数据结构是面向对象的,并且无法将SVG对象添加到类中,因为SVG对象已添加到DOM中。 So I change it so that it work's procedural. 因此,我对其进行了更改,以使其在程序上起作用。

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

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