最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

svg Polygon Points - Javascript - Not Working - Stack Overflow

programmeradmin3浏览0评论

How can I control the points of a svg polygon via JavaScript? Tried solving it this way, but it does not work.

function points() {
  document.getElementById("polygon").points = "0,100 0,0 100,100";
}
<svg width="100" height="100">
  <polygon id="polygon" points="0,0 100,0 100,100" style="fill:#000;">
</svg>
  
<input type="button" onClick="points" value="Change Polygon Points">

How can I control the points of a svg polygon via JavaScript? Tried solving it this way, but it does not work.

function points() {
  document.getElementById("polygon").points = "0,100 0,0 100,100";
}
<svg width="100" height="100">
  <polygon id="polygon" points="0,0 100,0 100,100" style="fill:#000;">
</svg>
  
<input type="button" onClick="points" value="Change Polygon Points">

Share Improve this question edited Sep 20, 2015 at 13:11 JakeTheSnake asked Sep 20, 2015 at 12:32 JakeTheSnakeJakeTheSnake 2,4643 gold badges16 silver badges27 bronze badges 1
  • That's basically the same polygon so you'd never know whether the function did anything. – Robert Longson Commented Sep 20, 2015 at 12:38
Add a ment  | 

2 Answers 2

Reset to default 4

I've changed the output polygon so that it looks different to the starting polygon, otherwise it looks like the code doesn't do anything.

function points() {
  document.getElementById("polygon").setAttribute("points", "100,0 50,0 100,100");
}
<svg width="100" height="100">
  <polygon id="polygon" points="0,0 100,0 100,100" style="fill:#000;">
</svg>
  
<input type="button" onClick="points()" value="Change Polygon Points">

document.getElementById('polygon1').setAttribute('points', '100, 0 50, 0 100, 100');
发布评论

评论列表(0)

  1. 暂无评论