简体   繁体   English

HTML CSS - 单击时使图标更改颜色(如链接)

[英]HTML CSS - Make icon change color when clicked (like a link)

When you click a link, typically it changes to purple, so you know that you've clicked that link.当您单击一个链接时,它通常会变为紫色,因此您知道您已经单击了该链接。 Usually resetting once you refresh or leave the page.通常在刷新或离开页面后重置。 Is there a way to make an icon (SVG or ) act like a link when clicked?有没有办法让一个图标(SVG 或 )在点击时表现得像一个链接? And not just the last one clicked.而不仅仅是最后一个点击。 It would be every single icon that gets clicked.这将是被点击的每一个图标。 I was trying to use the:visited pseudo, but it wasn't working.我试图使用:visited 伪,但它不起作用。

 .visit { fill: lightblue; }.visit:visited { fill: green; }
 <a href="#"><svg> <rect width='300' height='100' class='visit'></rect> </svg></a>

jQuery and JavaScript answers are fine, but if there's a way to do this with just HTML and CSS, that would be preferred. jQuery 和 JavaScript 的答案很好,但如果有办法只用 HTML 和 Z2C56C360580485DFBED23 来做到这一点,那7242 会是首选。

:visited is a link-related pseudo-class. :visited是一个链接相关的伪类。 It must be applied to the <a> tag.它必须应用于<a>标记。

 .visit { fill: lightblue; } a:visited.visit { /* Fix here */ fill: green; }
 <a href="#"><svg> <rect width='300' height='100' class='visit'></rect> </svg></a>

Like this and use a different number after # for every element you want to use this on.像这样并在 # 之后为要使用它的每个元素使用不同的数字。

 .visit { fill: lightblue; } a:visited.visit { fill: green; }
 <a href="#1"><svg> <rect width='300' height='100' class='visit'></rect> </svg></a>

If I understood your question correctly, you can use the active pseudo for this.如果我正确理解了您的问题,您可以为此使用active伪。 This will make it so that when the link is active (ie clicked) turn green in this case.在这种情况下,这将使它在链接处于活动状态(即单击)时变为绿色。

For more information on the active pseudo, please see this site有关active伪的更多信息,请参阅站点

 a { padding: 10px; }.visit { fill: lightblue; }.visit:active { fill: green; }
 <a href="#"><svg> <rect width='300' height='100' class='visit'></rect> </svg></a><a href="#"><svg> <rect width='300' height='100' class='visit'></rect> </svg></a><a href="#"><svg> <rect width='300' height='100' class='visit'></rect> </svg></a><a href="#"><svg> <rect width='300' height='100' class='visit'></rect> </svg></a><a href="#"><svg> <rect width='300' height='100' class='visit'></rect> </svg></a>

Here is a working sample!这是一个工作示例!

 .box { width: 300px; height: 300px; }.box * { width: 100%; height: 100%; }.box.visit { fill: #ccc; }.box:link.visit { fill: #999; }.box:hover.visit { fill: #3f9; }.box:active.visit { fill: #f93; }.box:visited.visit { background: #39f; }
 <a href="#" class='box'> <svg> <rect class='visit'> </rect> </svg> </a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM