Jquery改变表格单元格颜色

Mik*_*ike 0 html css jquery html-table colors

我一直试图让自己的工作,现在已经放弃了.我从SO和其他网站上搜索了大部分代码.

我想要做的是根据我上一次的价值改变background-color我的第<td>一个<td>

这就是我目前所拥有的,但我似乎无法获得除第一种之外的任何其他颜色if statement:(

$(document).ready(function()
 {  
             $("#tableData tr:not(:first)").each(function() { 

                 //get the value of the table cell 
                 var Colour = $(this).find("td:nth-child(4)").html();

                    alert(Colour);


                 //check the colour
                 if (Colour = "red")
                 {
                     //change the color of the background
                     $(this).find("td:nth-child(1)").css("background-color", "red");
                 }
                 else if (Colour = "green")
                {
                    $(this).find("th:nth-child(1)").css("background-color", "green");
                }
                 else if (Colour = "blue")
                {
                    $(this).find("th:nth-child(1)").css("background-color", "blue");
                }

             });
 });
Run Code Online (Sandbox Code Playgroud)

我还为你创建了一个FIDDLE,看看我遇到了什么.请你根据单元格值让我知道我是如何动态更改的?

谢谢!麦克风

小智 5

这是你想要做的吗?你不需要if语句.只是将Colour值传递给background-color这样

$(this).find("td:nth-child(1)").css("background-color", Colour);
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/5gcvoqfn/2/