简体   繁体   中英

Highlighting Table Rows does not work

I have a HTML table a jquery script as given in http://jsfiddle.net/Lijo/Hb28u/4/ . There are four jQuery approaches for highlighting table rows. The last two approaches does not work, Why are they not working? I am looking for an explanation in plain English.

HTML

<table id="table1">
<tr> <td>N</td><td>Y</td></tr>  
<tr class="y_n"><td>Q</td><td>N</td></tr>  
</table> 

 <br/><br/>

<table id="table2">
<tr> <td>N</td><td>Y</td></tr>  
<tr class="y_n"><td>Q</td><td>N</td></tr> 
</table> 

<br/><br/>

<table id="table3">
<tr> <td>N</td><td>Y</td></tr>  
<tr class="y_n"><td>Q</td><td>N</td></tr>  
</table> 

 <br/><br/>

<table id="table4">
<tr> <td>N</td><td>Y</td></tr>  
<tr class="y_n"><td>Q</td><td>N</td></tr>  
 </table> 

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

SCRIPT

$(document).ready(function()
{


//Apporach 1  - Highlight First Row

$('#table1 tr td:eq(0)').each(function() {
if ($(this).text() == 'N') {
    $(this).parent().css('background-color', 'Orange');
}
});


//Apporach 2 -  - Highlight Second Row

$('#table2 tr td:gt(0)').each(function() {
if ($(this).text() == 'N') {
    $(this).parent().css('background-color', 'Orange');
}
});


//Apporach 3 - Highlight Second Row

$('#table3 tr td:eq(1)').each(function() {
if ($(this).text() == 'N') {
    $(this).parent().css('background-color', 'Orange');
}
});


//Apporach 4 Highlight All Rows

$('#table4 tr td)').each(function() {
if ($(this).text() == 'N') {
    $(this).parent().css('background-color', 'Orange');
}
});

});

Now it works jsfiddle

$('#table3 tr td:eq(3)').each(function() { // note that you search for td no. 3 and not 1
   if ($(this).text() == 'N') {
      $(this).parent().css('background-color', 'Orange');
   }
});


//Apporach 4 Highlight All Rows
$('#table4 tr td').each(function() { // note that in your example you have a ) at the end of the selector
    if ($(this).text() == 'N') {
        $(this).parent().css('background-color', 'Orange');
    }
});
 //Apporach 3 - Highlight Second Row
$('#table3 tr:eq(1) td').each(function() { //second row
if ($(this).text() == 'N') {
    $(this).parent().css('background-color', 'Orange');
}
});


    //Apporach 4 Highlight All Rows
    $('#table4 tr td').each(function() { //here you had a unnescessary ')'
    if ($(this).text() == 'N') {
        $(this).css('background-color', 'Orange');
    }
    });

hope you got it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM