2011-10-05 157 views
2

嗨,我希望有人可以幫助我調整此JQuery代碼,以便它將突出顯示整個數據行而不是僅包含值'N'的單元格。我試圖將代碼應用到表格行,但它仍然只突出顯示包含'N'值的單元格的背景顏色,因爲我需要突出顯示整個表格行。有人有任何建議嗎?jquery根據列值突出顯示行

<html> 
    <head> 
    <script type="text/javascript" 
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script> 
    <script type="text/javascript"> 

    $(document).ready(function(){ 
         $('#table_id tr.y_n td').each(function(){ 
                  if ($(this).text() == 'N') { 
                   $(this).css('background-color','#f00'); 
                   } 
                   }); 
         }); 

    </script> 

    </head> 
    <body> 
    <table id="table_id"> 
     <tr><th>Question</th><th>Y/N?</th></tr> 
     <tr><td>I am me.</td><td>Y</td></tr> 
     <tr class="y_n"><td>N</td><td>Y</td></tr> 
     <tr><td>I am not sure.</td><td class="y_n">Y</td></tr> 
     <tr><td>This is a table.</td><td class="y_n">Y</td></tr> 
    </table> 

    </body> 
    </html> 

回答

1

我相信parent()是你想要的。更改以下行:

$(this).css('background-color','#f00'); 

$(this).parent().css('background-color','#f00')