2011-10-05 162 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')