如何使SVG组中的所有元素更改onclick(js)的颜色

时间:2019-05-15 12:49:05

标签: javascript function svg colors setattribute

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

我尝试使用onclick内联svg。它似乎没有用。所以我从javascript开始。现在,它只填充一个trianlge,而我将函数设置为从组中调用。

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>

我希望单击该组中的任何元素后,该组中的所有元素都会更改为另一种颜色,并使它们保持该颜色。

4 个答案:

答案 0 :(得分:2)

这有效

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>

答案 1 :(得分:1)

虽然您所做的是正确的,但是您尝试对组而不是组中的元素应用填充-简单地循环单击所单击的svg中的元素将允许您将它们填充为所有相同的颜色。我使用了for循环来获取浏览器支持。

还值得注意的是,单击后,直到将鼠标移离元素后,它才会显示,因为尽管设置了填充,但CSS中仍然设置了hover属性。

function callred(){
    const children = document.getElementById('btn1').children;
  for(let i = 0; i < children.length; i++ ){
    children[i].setAttribute('fill','#ff00ff');
  }
}
g:hover > polygon{
    fill: yellow;
}
b:hover > polygon{
    fill: yellow;
}

#svg-object{
    
        height: 100vh;    
        width: 100%;
        background-size: cover;
        background-position: center center;
        border: 15px antiquewhite;
        position: absolute;
    
}
<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>

答案 2 :(得分:0)

使用document.getElementsByTagName('polygon')获取所有多边形,然后在它们上循环,设置每个多边形的填充度:

function callred(){
var els = document.getElementsByTagName('polygon')
  for (var i=0; i < els.length; i++) {
      els[i].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>

答案 3 :(得分:0)

我个人可能只是切换一个css类来打孩子;

function callred(){
   document.getElementById("btn1").classList.toggle("new-style");
}
#svg-object{
    
        height: 100vh;    
        width: 100%;
        background-size: cover;
        background-position: center center;
        border: 15px antiquewhite;
        position: absolute;
    
}

.new-style polygon {
  fill: red!important;
}
<?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>