我想在 javascript 中获取 4 个按钮,以便在单击它们时更改 svg 中正方形的颜色
到目前为止,我有以下代码
<html>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="100%"height="100%"version="1.1"
<svg xmlns="http://www.adobe.com/svg">
<script type="text/ecmascript">
<![CDATA[
function chooseColor1() {
document.getElementsById("mySqaure")[0].style.color = "green";
}
function chooseColor2() {
document.getElementsById("mySqaure")[0].style.color = "pink";
}
function chooseColor3() {
document.getElementsById("mySqaure")[0].style.color = "purple";
}
function chooseColor4() {
document.getElementsById("mySqaure")[0].style.color = "blue";
}
]]>
</script>
`<g
id="firstGroup">
<rect id = "mySquare" x="0" y="0" width="20" height="20" fill="grey" stroke="black" stroke-width="2" />
</g>
<form onsubmit="return false">
<input type="button" value="green" onclick="chooseColor1()"/>
<input type="button" value="pink" onclick="chooseColor2()"/>
<input type="button" value="purple" onclick="chooseColor3()"/>
<input type="button" value="blue" onclick="chooseColor4()"/>
</form>
</svg>
</html>