交互の色(黄色と赤など)の行を持つテーブルを作成しました。ここで、クリックされた行の色を 1 つの一般的な色 (たとえば青) に変更したいと思います。もう一度クリックすると、元の色に戻ります。このコードを使用して色を変更できます
$("#mainTable").find('#'+IDClicked).css("background-color", "#bbbbff");
元に戻す方法がわかりません。
交互の色(黄色と赤など)の行を持つテーブルを作成しました。ここで、クリックされた行の色を 1 つの一般的な色 (たとえば青) に変更したいと思います。もう一度クリックすると、元の色に戻ります。このコードを使用して色を変更できます
$("#mainTable").find('#'+IDClicked).css("background-color", "#bbbbff");
元に戻す方法がわかりません。
私たちはあなたのコードがこのようになっていると仮定します:
<table id="rowClick">
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
</table>
$(document).ready(function(){
$("#rowClick > tr").click(function(){
$(this).toggleClass("active");
});
});
table tr.active {background: #ccc;}
あなたの質問に正確に答える:
$('#box').on('click', function() {
var box$ = $(this),
isBgColorChanged = box$.data('isBgColorChanged'),
bgColor = !!isBgColorChanged ? '#444' : '#ccc';
box$.css('background-color', bgColor)
.data('isBgColorChanged', !isBgColorChanged);
});
よりエレガントなソリューション:
$('#box').on('click', function() {
$(this).toggleClass('activated');
});
以下のコードを試してください
CSS
.high-light{background:blue !important;}
jQuery
$(document).ready(function(){
$('table tr').click(function(){ $(this).addClass("high-light"); });
//If you have TD's background set try the below commented code
$('table tr td').click(function(){ $(this).parent().find('td').addClass("high-light"); });
});
このデモをお試しください:
$('table tr').toggle(function(){
$(this).addClass('myclass');
}, function(){
$(this).removeClass('myclass');
});
css
.myclass{
background: red;
}
table tr{
background: yellow;
}