如果您给折线添加一个id属性,使它看起来像这样:
<polyline id="polyline-id" points="0, 240 40, 25 60, 40 80, 120 120, 140 200, 180 500, 120 600, 5" style="fill:none;stroke:#000;stroke-width:3" />
您可以通过document.getElementById
获取对它的引用。
最简单的方法是通过对“points”属性使用getAttribute/setAttribute来操作它。
var points = polyline.getAttribute("points");
points += "10, 20";
points.setAttribute("points", points);
但是最高性能的选项是SVG DOM,因为它避免了将点数据序列化为/从字符串 - 我所知道的所有UA都将数据存储为浮点数或双精度数组,而不是字符串。
var svg = document.getElementById('svg');
var point = svg.createSVGPoint();
point.x = 10;
point.y = 20;
var polyline= document.getElementById('polyline-id');
polyline.points.appendItem(point);
polyline.setAttribute('points', points);
。 - Jordan