TK4*_*421 5 html svg hta polyline
如何使用JavaScript为现有SVG折线添加坐标?
<svg height="240" width="700" id='svg'>
<polyline 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" />
</svg>
Run Code Online (Sandbox Code Playgroud)
我正在使用IE 9(在.hta文件中)并且需要能够动态地将新点附加到折线.目标是能够创建折线图.我提前道歉,我完全不知道如何引用这个属性.对此的任何指导将不胜感激.
如果向折线添加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" />
Run Code Online (Sandbox Code Playgroud)
你可以通过它获得它的参考 document.getElementById
最简单的方法是通过"points"属性上的getAttribute/setAttribute对其进行操作
var points = polyline.getAttribute("points");
points += "10, 20";
points.setAttribute(points);
Run Code Online (Sandbox Code Playgroud)
但最高性能的选项是SVG DOM,因为它避免了将数据串行化到字符串中或从字符串中串行化 - 我所知道的所有UAs都在内部将数据存储为浮点数或双精度数而不是字符串数.
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);
Run Code Online (Sandbox Code Playgroud)