2011-08-24 23 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)";