jquery:even:奇怪没有按预期工作IE10

时间:2013-05-06 03:19:00

标签: jquery internet-explorer

在包含表格的页面上

在$(document).ready()中,我有以下代码:

   populateTable();  // creates and appends TR's to the table from in-memory vars
   $("#table1Body tr:even").css("background-color", "rgb(181,221,181)");
   $("#table1Body tr:odd").css("background-color", "rgb(255,255,220)");    

这在Firefox,Chrome和Safari中可以正常运行,但是......

在IE10中,只有少数(有时没有)表行交替着色 直到我将鼠标移动到屏幕上并再次移出桌面,然后再移动。 我第一次将鼠标移动到表格中时,一些行获得了正确的结果 背景颜色,但有些不。当我将鼠标移出并再次移回时 行得到正确的背景。

populateTable函数不执行任何可能导致与任意完成相关的问题的ajax或其他任何东西,它只是将内存中的变量加载到s中并将它们附加到。为了验证这一点,我将代码捆绑到一个函数中并从setTimeout执行函数,延迟时间为5秒 - 这并没有改变一件事。

有什么想法吗?

2 个答案:

答案 0 :(得分:5)

使用:nth-child()伪选择器。

tr:nth-child(odd) {
    background-color: rgb(181,221,181);
}

tr:nth-child(even) {
    background-color: rgb(255,255,220);
}

小提琴:http://jsfiddle.net/Sn6R5/

参考:https://developer.mozilla.org/en-US/docs/CSS/:nth-child

所以你的jQuery选择器会是这样的:

$("#table1Body tr:nth-child(odd)").css("background-color", "rgb(181,221,181)");

答案 1 :(得分:0)

您在第一个rgb()行上缺少一个括号。

试试这个。

populateTable();  // creates and appends TR's to the table from in-memory vars
$("#table1Body tr:even").css("background-color", "rgb(181,221,181)");
$("#table1Body tr:odd").css("background-color", "rgb(255,255,220)");    

IE在检测这类人为错误方面不是最好的。