简体   繁体   English

单击链接时更改背景颜色

[英]Changing the background color when a link is clicked

I need to use.onclick method to change the color of a link when clicked but the problem is when i click on another link the first link color don't return back to the original color, so ny suggestions?我需要使用.onclick 方法来更改单击时链接的颜色,但问题是当我单击另一个链接时,第一个链接颜色不会返回到原始颜色,所以有什么建议吗?

I also believe a CSS solution is better, but if you really want an onClick method, you can try the following:我也相信 CSS 解决方案更好,但如果您真的想要 onClick 方法,您可以尝试以下方法:

 const one = document.getElementById("one"); const two = document.getElementById("two"); function updateOne() { reset(); one.style.color = "red"; } function updateTwo() { reset(); two.style.color = "red" } function reset() { one.style.color = "blue"; two.style.color = "blue"; }
 <html> <body> <a href="#" onClick="updateOne()" id="one">Link One</a> <br> <a href="#" onClick="updateTwo()" id="two">Link Two</a> </body> </html>

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

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