2015-09-18 59 views
0

此代碼將顯示一個表格單元格。當我點擊表格單元格時,背景顏色會提示rgb(173,216,230),但我想檢查該顏色,如果它是那種顏色,那麼請注意它的工作原理。我的支票總是失敗。任何人都知道如何解決這個問題?如何檢查jQuery中背景顏色的值?

<html> 
<head> 
<script src="jquery-1.11.2.min.js"></script> 
<script> 

$(document).ready(function(){ 

    $("td").click(function(){ 
     alert($(this).css('background-color')); 
     if ($(this).css('background-color') === 'rgb(173,216,230)') alert("matched"); 

    }); 
}); 
</script> 

<style> 

button{color: black} 
button:hover{background-color: lightblue} 
td{color: black;width:30px; height:30px;text-align:center;} 
td:hover{background-color: lightblue} 
table { 
    border-collapse: collapse; 
} 

table, td, th { 
    border: 1px solid black; 
    background-color: yellow; 
} 
</style> 

</head> 
<body style='padding-left: 1cm;'> 

<table> 
<tr> 
<td>test</td> 
</tr> 
</table> 

</body> 
</html> 
+1

'RGB(173,216,230)'缺少空格。你應該使用數據屬性或類似的東西。嘗試解析CSS中的CSS屬性並不好玩。 – Joe

回答

1

你缺少空格:

background-color:rgb(173, 216, 230); 


if($(this).css('background-color') == 'rgb(173, 216, 230)') { 
    alert("matched"); 
} 

http://jsfiddle.net/9f5jwxpo/