简体   繁体   中英

How to change the color of a button when clicked, and when other button is clicked

I have an html site with sections. On the top of the site, I have buttons with link to every section Default color: white . I want, when I click on a button to change it's color, but also, when I click on ANOTHER button, to change the color of the other button, and make the old button color white again.

I tried some script examples from stackoverflow, but I never find something working like what I wanted.

you can use a code like this:

function changeFirstButtonColor() {
 document.getElementById("first-btn").style.backgroundcolor = 'new color';
 document.getElementById("second-btn").style.backgroundcolor = 'old color';
}

function changeSecondButtonColor() {
 document.getElementById("second-btn").style.backgroundcolor = 'new color';
 document.getElementById("first-btn").style.backgroundcolor = 'old color';
}

And call the function for the good button with onclick event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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