单击时更改html svg cricle填充css

zom*_*bom 5 html css svg

我使用以下svg绘图作为按钮:

<svg id="button-more-people" style="width: 60px; height: 60px;">
    <circle cx="30" cy="30" r="30" fill="#F44336" id="circle-button-more-people"/>
    <text fill="#ffffff" font-size="45" x="11" y="44" font-family="Verdana">+</text>
</svg>
Run Code Online (Sandbox Code Playgroud)

当鼠标悬停在圆圈上时,我通过css按下按钮:

#button {
    transition: 0.3s ease;
}

#button:hover {
    transform: scale(1.2);   
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法实现的是在点击时更改按钮颜色.我试过以下,但无济于事:

#button:active ~ circle {
    fill: #D50000;
}
Run Code Online (Sandbox Code Playgroud)

我希望如果有一个没有javascript的解决方案我可以使用.

在此先感谢您的帮助!

Isa*_*sac 5

问题出在选择器用于fill:

#button:active ~ circle {
Run Code Online (Sandbox Code Playgroud)

这是General Sibling Combinator,在你的情况下匹配<circle>兄弟姐妹的标签#button,然后在你的标记中<circle>是一个孩子的#button

你可以通过使用后代组合子来解决这个问题:

#button:active circle {
Run Code Online (Sandbox Code Playgroud)

或者儿童组合子:

#button:active > circle {
Run Code Online (Sandbox Code Playgroud)

例:

#button-more-people {
  transition: 0.3s ease;
}

#button-more-people:hover {
  transform: scale(1.2);
}

#button-more-people:active circle {
  fill: #D50000;
}
Run Code Online (Sandbox Code Playgroud)
<svg id="button-more-people" style="width: 60px; height: 60px;">
    <circle cx="30" cy="30" r="30" fill="#F44336" id="circle-button-more-people"/>
    <text fill="#ffffff" font-size="45" x="11" y="44" font-family="Verdana">+</text>
</svg>
Run Code Online (Sandbox Code Playgroud)