Как добавлять атрибуты к через JS?
Нужно добавить атрибуты х1, у1, х2 и у2 к <line> через JavaScript.
Так не работает:
for(let i = 0; i < 10; i++) {
let id_num = 0;
let nLine = document.createElement("line");
nLine.id = id_num + "line";
nLine.x1 = 1;
nLine.y1 = 1;
nLine.x2 = 2;
nLine.y2 = 2;
document.getElementById("svg").appendChild(nLine);
id_num++;
}
Ответы (1 шт):
Автор решения: Kirill
→ Ссылка
Добавьте setAttribute
nLine.setAttribute('id', id_num + 'line');
nLine.setAttribute('x1', 1);
nLine.setAttribute('y1', 1);
nLine.setAttribute('x2', 2);
nLine.setAttribute('y2', 2);