html - 单击时更改 html svg cricle 的 css 填充

标签 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>

当鼠标悬停在圆上时,我通过 css 将按钮放大:

#button {
    transition: 0.3s ease;
}

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

我似乎无法实现的是在单击按钮时更改按钮颜色。我尝试了以下方法,但无济于事:

#button:active ~ circle {
    fill: #D50000;
}

如果有一个我可以使用的无 javascript 解决方案,我会更愿意。

提前感谢您的帮助!

最佳答案

问题出在用于 fill 的选择器中:

#button:active ~ circle {

这是 General Sibling Combinator在您的情况下匹配 <circle> #button 之后的兄弟标签,但是在您的标记中 <circle>#button 的 child

您可以使用后代组合器解决此问题:

#button:active circle {

或者子组合器:

#button:active > circle {

例子:

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

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

#button-more-people:active circle {
  fill: #D50000;
}
<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>

关于html - 单击时更改 html svg cricle 的 css 填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46248751/

相关文章:

javascript - jQuery 绑定(bind)事件不会在通过 $().load() 加载的元素上触发

javascript - 使用 JavaScript 将 HTML 内容替换为外部 XML 文件中的内容?

javascript - 为什么 'document.querySelectorAll' 只针对直系 child ?

html宽度大小写

javascript - 显示标签和布局的 Bootstrap 问题

javascript - 如何更轻松地切换 .click?

html - 文本溢出与 flexbox 结合不起作用

SVG | y坐标不同

javascript - 如何在D3.js中平滑地绘制从起点到终点的路径

javascript - 使 SVG 图形在进入窗口时播放(滚动)