$.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>