2011-08-24 72 views
0
table#id_table_comments tr td { 
    /*background: #fff;*/ 
    background: #f5f5f5; 
    padding: 6px; 
    text-align: left; 
    vertical-align: top; 
    border-bottom:1px solid #ddd; 
} 
table#id_table_comments tr:nth-child(2n) td { 
    /*background: #f5f5f5;*/ 
    background: #fff; 
} 
.classTableRow{ 
    background-color: #9999CC; 
    border: 1px solid gray; 
} 



$(document).ready(function(){ 
    $("td").mouseover(function(){ 
     $(this).addClass('classTableRow'); 
    }) 

    $("td").mouseout(function(){ 
     $(this).removeClass('classTableRow'); 
    }) 
}); 

但jQuery不适用于第n行(偶数行)。jQuery帮助:如何选择tr td第n行?

我该怎么办?

回答

3

table#id_table_comments tr:nth-child(2n) td.classTableRow更具体,所以其背景胜出。
!important添加到.classTableRow背景强制它覆盖其他选择器。

此外,你应该使用:hover而不是使用jQuery来添加一个类。

+0

mouseover和mouseout有什么问题?为什么我应该使用:悬停? – shibly

+0

他们比较慢。总是最好使用CSS而不是Javascript。他们也错了(他们泡沫);如果你真的想使用Javascript,你应该使用'mouseenter'和'mouseleave'。 – SLaks

+0

mouseover/mouseout和mouseenter/mouseleave有什么区别? – shibly

0

如果我理解正确,您希望在表格中的特定TR上选择特定的TD。

如果是这样,请收集行索引并添加一行(如果您不想计算标题)。然后选择特定的细胞很容易。

var row = $(this).closest('tr').index() + 1; 
thisRow = "table tr:nth-child(" + row + ")"; 
thisCell = thisRow + " td:nth-child(N)";