简体   繁体   中英

Javascript how to Toggle button background color on click

can some one tell me how to Toggle button background color on click?

look at this buttons and add examples please : http://jsfiddle.net/ukkj7dth/

<div class="button">button 1</div>
<div class="button">button 2</div>
<div class="button">button 3</div>
<div class="button">button 4</div>
<div class="button">button 5</div>
<div class="button">button 6</div>

i want it to be like, when a button 1 clicked it must change background color to yellow and when button 2 clicked then button 1 must go back to green background and button 2 change to yellow etc...

Thanks

Just toggle a class

$('.button').on('click', function() {
    $(this).toggleClass('myClass').siblings().removeClass('myClass');
});

FIDDLE

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