Итак, у меня есть элемент SVG, который является текстом. Я хотел бы динамически создавать больше текстовых элементов SVG одного и того же вида, используя JavaScript. (желательно используя цикл for какого-то вида). Один из вариантов - просто жестко закодировать значения, но я бы этого не делал. Вот мой код:
var overlapThreshold = "50%";
var name_count = 0;
Draggable.create(".seat_name", {
bounds: "svg",
onDrag: function(e) {
if (this.hitTest("#test1", overlapThreshold)) {
document.getElementById("test1").setAttribute('fill', 'url(#gradRed)');
} else {
document.getElementById("test1").setAttribute('fill', 'url(#gradGreen)');
}
}
});
function change_name(event) {
var name = prompt("Enter a New Name:");
if (name != null && name != "") {
event.target.textContent = name;
}
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<button id="test_button" onclick="create_name_tags()">Test</button> <svg height="1000" width="1000">
<defs>
<lineargradient id="gradGreen" x1="0%" x2="100%" y1="0%" y2="0%">
<stop offset="0%" style="stop-color:rgb(152, 251, 152);stop-opacity:1"></stop>
<stop offset="100%" style="stop-color:rgb(0, 128, 0);stop-opacity:1"></stop>
</lineargradient>
<lineargradient id="gradYellow" x1="0%" x2="100%" y1="0%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255, 140, 0);stop-opacity:1"></stop>
<stop offset="100%" style="stop-color:rgb(218, 165, 32);stop-opacity:1"></stop>
</lineargradient>
<lineargradient id="gradRed" x1="0%" x2="100%" y1="0%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255, 0, 0);stop-opacity:1"></stop>
<stop offset="100%" style="stop-color:rgb(178, 34, 34);stop-opacity:1"></stop>
</lineargradient>
</defs>
<g class="circle_seat" id="circle_seats">
<circle cx="70" cy="200" fill="url(#gradGreen)" id="test1" id="seat1" r="40" stroke="black" stroke-width="1"></circle>
</g>
<g class="seat_name" id="seat_name1">
<text fill="#black" font-family="Verdana" font-size="20" id="seat1_details" ondblclick="change_name(event)" x="250" y="210">
BLANK
</text>
</g>
</svg>
</body>
</html>