简体   繁体   中英

svg dom use variables to define object attributes

can you help me? I want to use javascript in order to alter the form of a svg polygon using given variables. I tried this:

var example = 200;
document.getElementById('myPolygon').setAttribute('points','50,50 50,100 example,100');

It does not work. The double quotation mark doesn't work, either. However, I need the variable because it changes over time. What is the right syntax in this case?

I have looked for such a question, but didn't find it. If there is already a solution for my question provided on this forum, please kindly let me know the link to it. Thank you!

try this.you should append the value of variable example between the string. if you use '50,50 50,100 example,100' ,example is considered as a part of string. '50,50 50,100 '+example+',100' here you are adding value of variable example to string.

document.getElementById('myPolygon').setAttribute('points','50,50 50,100 '+example+',100');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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