简体   繁体   English

单击按钮时更改按钮颜色

[英]Change Button Color when click on the Button

I have many button in a row in my page. 我的页面上连续有很多按钮。 And I need to change the button color, when I click on the Buttons. 当我单击“按钮”时,我需要更改按钮的颜色。 Here is My sample code 这是我的示例代码

 $( "button.change" ).click(function() { $(this).toggleClass( "selected" ); }); 
 .Button { font-family: Calibri, sans-serif; font-size:13px; font-weight: bold; width: 160px; height: 25px; background:grey; color: white } .selected { color: white; background:green } button#cssColorChange:active{ color: white; background:red } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <button class="Button change" id="jQueryColorChange">Click to change color</button> <br><br> <button class="Button change" id="jQueryColorChange">Click to change color</button> <br><br> <button class="Button change" id="jQueryColorChange">Click to change color</button> <br><br> <button class="Button change" id="jQueryColorChange">Click to change color</button> 

Here it is Working. 在这里工作。 But here when I click on every buttons, color has been changed. 但是在这里,当我单击每个按钮时,颜色已更改。 I need, When I click on one Button, color should change for that Button only and the remaining Buttons color should default. 我需要,当我单击一个按钮时,该按钮的颜色应仅更改,其余按钮的颜色应为默认颜色。

You need to remove the selected class on all the buttons (which clears the styling of the previously selected button) and then apply it to the clicked one. 您需要在所有按钮上删除选定的类(这将清除先前选定按钮的样式),然后将其应用于单击的按钮。 Also the buttons need to have unique ID's or none at all( the clas is sufficient for this function) and you should space the buttons out with CSS rather than the double line breaks. 同样,按钮需要具有唯一的ID或根本没有ID(此功能足以满足此要求),并且您应该使用CSS而不是双换行符来分隔按钮。

 $("button.change" ).click(function() { $(".change" ).removeClass('selected'); $(this).addClass( "selected" ); }); 
 .Button { font-family: Calibri, sans-serif; font-size:13px; font-weight: bold; width: 160px; height: 25px; background:grey; color: white; display:block; margin: 1em; } .selected { color: white; background:green } button#cssColorChange:active{ color: white; background:red } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <button class="Button change">Click to change color</button> <button class="Button change">Click to change color</button> <button class="Button change" >Click to change color</button> <button class="Button change">Click to change color</button> 

Something like this? 像这样吗

 $( "button.change" ).click(function() { $( "button.change.selected" ).removeClass("selected"); $(this).toggleClass( "selected" ); }); 
 .Button { font-family: Calibri, sans-serif; font-size:13px; font-weight: bold; width: 160px; height: 25px; background:grey; color: white } .selected { color: white; background:green } button#cssColorChange:active{ color: white; background:red } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <button class="Button change" id="jQueryColorChange">Click to change color</button> <br><br> <button class="Button change" id="jQueryColorChange">Click to change color</button> <br><br> <button class="Button change" id="jQueryColorChange">Click to change color</button> <br><br> <button class="Button change" id="jQueryColorChange">Click to change color</button> 

Try this JQuery code instead 试试这个JQuery代码

$( "button.change" ).click(function() {
  $('button').removeClass( "selected" );
  $(this).toggleClass( "selected" );
});

What this does is, first removes .selected class from every button and then applies the class to only the button which has been clicked. 它的作用是,首先从每个按钮中删除.selected类,然后将该类仅应用于已单击的按钮。

the above answers work very good . 以上答案效果很好。 but IF you want to disable the button when you click again on it, see snippet below 但是如果您想在再次单击该按钮时禁用该按钮,请参见下面的代码段

 $( "button.change" ).click(function() { $(this).toggleClass( "selected" ); $("button.selected").not(this).removeClass("selected") }); 
 .Button { font-family: Calibri, sans-serif; font-size:13px; font-weight: bold; width: 160px; height: 25px; background:grey; color: white } .selected { color: white; background:green } button#cssColorChange:active{ color: white; background:red } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="Button change" id="">Click to change color</button> <br><br> <button class="Button change" id="">Click to change color</button> <br><br> <button class="Button change" id="">Click to change color</button> <br><br> <button class="Button change" id="">Click to change color</button> 

First of all, please don't use the same id for multiple elements. 首先,请不要对多个元素使用相同的ID。 Id's should be unique. ID应该是唯一的。

$('button.change').click(function() {
  $(this).toggleClass('selected');
})

your code is working fine btw, https://jsbin.com/sicifopizi/edit 顺便说一句,您的代码工作正常, https: //jsbin.com/sicifopizi/edit

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

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