私は自分のページのボタンを一列に並べています。ボタンをクリックすると、ボタンの色を変更する必要があります。ここでは私のサンプルコードはボタンをクリックするとボタンの色が変わります
$("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>
ここでそれが働いています。しかし、ここですべてのボタンをクリックすると、色が変更されました。ボタンが1つしかクリックされていない場合は、ボタンの色のみが変更され、残りのボタンの色はデフォルトになります。