gpt4 book ai didi

javascript - 如何使svg组中的所有元素改变颜色onclick(js)

转载 作者:行者123 更新时间:2023-11-28 17:04:44 25 4
gpt4 key购买 nike

我想更改 svg 文件中组中所有元素的填充(onclick)。目前,我使用 javascript 将属性设置为 id。这似乎只改变了其中一个元素。

我尝试过使用 onclick 内联 svg。它似乎不起作用。所以我从 javascript 开始。现在,它只填充一个三 Angular 形,而我将函数设置为从组中调用。

function callred(){
document.getElementById('btn1').setAttribute('fill', '#ff00ff');
}
#svg-object{

height: 100vh;
width: 100%;
background-size: cover;
background-position: center center;
border: 15px antiquewhite;
position: absolute;

}
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg id="svg-object" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="800px" height="754px" viewBox="0 0 800 754" enable-background="new 0 0 800 754" xml:space="preserve">
<g id="btn1" onclick="callred()">
<polygon fill="#FF0013" points="366.699,131 410,56 453.301,131 "/>
<polygon fill="#07FF00" points="323.699,656 367,581 410.301,656 "/>
<polygon fill="#0000FF" points="409.699,656 453,581 496.301,656 "/>
<polygon points="366.699,581 410,656 453.301,581 "/>
</g>


</svg>

我希望当单击组中的任何元素时,组中的所有元素都会更改为另一种颜色,并保持这种颜色。

最佳答案

这有效

function callred() {

[...document.getElementById('btn1').querySelectorAll('*')].forEach((e) => {
e.setAttribute('fill', '#ff00ff');
});
}
#svg-object{

height: 100vh;
width: 100%;
background-size: cover;
background-position: center center;
border: 15px antiquewhite;
position: absolute;

}
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg id="svg-object" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="800px" height="754px" viewBox="0 0 800 754" enable-background="new 0 0 800 754" xml:space="preserve">
<g id="btn1" onclick="callred()">
<polygon fill="#FF0013" points="366.699,131 410,56 453.301,131 "/>
<polygon fill="#07FF00" points="323.699,656 367,581 410.301,656 "/>
<polygon fill="#0000FF" points="409.699,656 453,581 496.301,656 "/>
<polygon points="366.699,581 410,656 453.301,581 "/>
</g>


</svg>

不确定这是一个好的答案

您还可以使用 CSS

function callred() {
document.getElementById('btn1').classList.toggle("forcecolor");
}
.forcecolor * {
fill: blue;
}

#svg-object{

height: 100vh;
width: 100%;
background-size: cover;
background-position: center center;
border: 15px antiquewhite;
position: absolute;

}
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg id="svg-object" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="800px" height="754px" viewBox="0 0 800 754" enable-background="new 0 0 800 754" xml:space="preserve">
<g id="btn1" onclick="callred()">
<polygon fill="#FF0013" points="366.699,131 410,56 453.301,131 "/>
<polygon fill="#07FF00" points="323.699,656 367,581 410.301,656 "/>
<polygon fill="#0000FF" points="409.699,656 453,581 496.301,656 "/>
<polygon points="366.699,581 410,656 453.301,581 "/>
</g>


</svg>

关于javascript - 如何使svg组中的所有元素改变颜色onclick(js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56149758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com