2014-10-29 136 views
1

如果background color td == color1然後將td background-color更改爲color2更改td的背景顏色

<table width="100%" cellspacing="0" cellpadding="0" border="0" class="table table-condensed"> 
<tbody><tr><td style="background-color:#00FF60">1</td></tr></tbody></table> 

我的jQuery:

你所包含的jQuery
$(document).ready(function(){ 
    if($('td').css('background-color') == 'rgb(0, 255, 96)') { 
     $('td').css('background-color','red'); 
    } 
}) 

http://jsfiddle.net/voxtermen/c9yz5c5L/4/

+0

你還沒有添加jQuery庫。 – 2014-10-29 11:46:59

回答

1

第一。然後,每td元素循環和一套基於喜好background-color

嘗試:

$("td").each(function (index) { 

     if ($(this).css('background-color') == 'rgb(0, 255, 96)') { 
      $(this).css('background-color', 'red'); 
     } 
}); 

DEMO

1

你還沒有加入jQuery庫到的jsfiddle(從下拉菜單中添加它左側)。 還需要使用.each()迭代td秒,然後檢查background-colortd,請參見下面的代碼

$(document).ready(function(){ 
    $('td').each(function(){ 

     if($(this).css('background-color') == 'rgb(0, 255, 96)') { 
      $(this).css('background-color','red'); 
     } 
    }); 
}); 

DEMO

1

弗里斯特,包括jQuery庫;第二,刪除「$(文件)。就緒(函數(){」既然你選擇了「的onLoad」。將以下代碼到你的JavaScript部分

$('td').each(function(){ 
    if($(this).css('background-color') === 'rgb(0, 255, 96)') { 
     $(this).css('background-color','red'); 
    } 
});