私はこのようなクラスを持っています
<div class="Ratsheet-destination-detail"></div>
$(".Ratsheet-destination-detail").css("backgroundColor", #5B815B);
これで、「Ratsheet-destination-detail」の背景色が赤になります。
これに背景色があることを確認する方法と、背景色を「#616161」に変更した場合
ありがとう.......
私はこのようなクラスを持っています
<div class="Ratsheet-destination-detail"></div>
$(".Ratsheet-destination-detail").css("backgroundColor", #5B815B);
これで、「Ratsheet-destination-detail」の背景色が赤になります。
これに背景色があることを確認する方法と、背景色を「#616161」に変更した場合
ありがとう.......
色は RGB 値として返されます。そのため、背景色の RGB 値を確認してください。赤の場合は、rgb(255, 0, 0)
緑に変更します。
var el = $('.Ratsheet-destination-detail');
if(el.css('background-color') == 'rgb(255, 0, 0)'){
el.css('background-color','green');
}
私が正しく理解すれば、opの編集でこれはうまくいくはずです。背景が赤または灰色の場合、これは緑に変わります。
var el = $('.Ratsheet-destination-detail');
if(el.css('background-color') == 'rgb(97, 97, 97)' || el.css('background-color') == 'rgb(255, 0, 0)'){
el.css('background-color','green');
}
この$.css
メソッドは、無名関数内から古い色が何であるかを教えてくれます。
$(".Ratsheet-destination-detail").css("background-color", function( index, old ){
// If current is red, set to green, else set to red
return $.Color(old).is("red") ? "green" : "red" ;
});
ここでは、色の操作を支援するためにjQuery Color pluginを使用しています。$.Color()
これがないと、RGB (または場合によっては RGBA) 形式で色を処理する必要があり、場合rgb(255, 0, 0)
によっては少し混乱する可能性があります。
デモ: http://jsbin.com/egemaf/2/edit
jQuery Color プラグインを使用するには、jQuery の場合と同様に、プロジェクトからソースをダウンロードして参照する必要があります (CDN を使用していないと仮定します)。
<!DOCTYPE html>
<html>
<head>
<title>Swapping Background Colors with jQuery and jQuery Color</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script src="https://raw.github.com/jquery/jquery-color/master/jquery.color.js"></script>
</head>
<body>
<div class="Ratsheet-destination-detail">
<p>Hello, World.</p>
</div>
<script>
$(function(){
$(".Ratsheet-destination-detail").css("background-color", function(i, old){
// If current is red, set to green, else set to red
return $.Color(old).is("red") ? "green" : "red" ;
});
});
</script>
</body>
</html>