views:

16

answers:

2
$(".wrap table tr:first").addClass("tr-top");

it works for the first table, but i have many tables under the div .wrap. what should i do? thanks!

A: 

This should work. The each loops through each table within .wrap.

$('.wrap table').each(function() {
    $('.wrap table tr:first').addClass('tr-top');
}};

http://api.jquery.com/each/

Catfish
This won't actually produce results any different from what the OP currently has.
patrick dw
thanks! but i think i've tried it as well seems doesn't work too me.
ray
A: 

In your example, this line returns the first in the set of all tr elements found.

$(".wrap table tr:first").addClass("tr-top");  // First <tr> of all that are found

So if you have 3 table elements, it will only return the first tr from the first table, since that will be the first tr element matched.

If you want the first tr for each table, you need first-child:

$(".wrap table tr:first-child").addClass("tr-top");  // First <tr> for each <table>

...which will return each tr that is a first child of its parent.

http://api.jquery.com/first-selector/

http://api.jquery.com/first-child-selector/

patrick dw
thanks you! that works.
ray
@Ray - You're welcome. :o)
patrick dw