Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery替换表的行和列颜色_Jquery_Html - Fatal编程技术网

使用jQuery替换表的行和列颜色

使用jQuery替换表的行和列颜色,jquery,html,Jquery,Html,我正在尝试使用以下代码来交替表中的行和列 <script> $(document).ready(function() { $("table#id2 td:even").css("background-color", "LightGray"); $("table#id2 tr:even").css("background-color", "LightBlue"); $("table#id2 tr:odd").css("background-color",

我正在尝试使用以下代码来交替表中的行和列

<script>
  $(document).ready(function()
  {
    $("table#id2 td:even").css("background-color", "LightGray");
    $("table#id2 tr:even").css("background-color", "LightBlue");
    $("table#id2 tr:odd").css("background-color", "LightYellow");
  });
</script>
我希望我的桌子看起来像这样:

Gray Blue
Gray Yellow
Gray Blue
Gray Yellow
Gray Blue
Yellow Yellow
Gray Blue
Yellow Yellow

这是否可能使用td:偶数、tr:odd等

基本上,仅对
奇数行应用颜色
尝试:


$(文档).ready(函数()
{
$(“table#id2 td:偶数”).css(“背景色”、“浅灰色”);
$(“table#id2 tr:even”).css(“背景色”、“浅蓝色”);
$(“table#id2 tr:odd td”).css(“背景色”、“淡黄色”);
});

基本上,仅对
奇数行应用颜色
尝试:


$(文档).ready(函数()
{
$(“table#id2 td:偶数”).css(“背景色”、“浅灰色”);
$(“table#id2 tr:even”).css(“背景色”、“浅蓝色”);
$(“table#id2 tr:odd td”).css(“背景色”、“淡黄色”);
});

您必须在
tr:odd td

$("table#id2 td:even").css("background-color", "LightGray");
$("table#id2 tr:even").css("background-color", "LightBlue");
$("table#id2 tr:odd td").css("background-color", "LightYellow");

这里有一把小提琴显示了一个解决方案:

您必须在
tr:odd td

$("table#id2 td:even").css("background-color", "LightGray");
$("table#id2 tr:even").css("background-color", "LightBlue");
$("table#id2 tr:odd td").css("background-color", "LightYellow");

这里有一把小提琴显示了一个解决方案:

为什么是jquery?为什么不是简单的css?看看这个链接为什么是jquery?为什么不简单的css?看看这个链接谢谢!当我尝试将td添加到显示为黄色的代码时,语法错误。谢谢!当我尝试将td添加到显示为黄色的代码时,语法错误。