我的页面中有一行中有很多按钮。当我点击按钮时,我需要改变按钮的颜色。下面是我的示例代码单击按钮时更改按钮颜色
$("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>
这是工作。但是当我点击每个按钮时,颜色已经改变。我需要,当我点击一个按钮时,颜色应该仅针对该按钮进行更改,其余按钮颜色应该默认设置。