2

I need to manually style the first row of every table on a page. Unfortunately I'm dealing with some old browsers so would like to do this via jQuery.

Currently I'm using:

$(function() {
 $(".OpenCourse tr:first").addClass("OpenCoursesHeader");
});

then adding .OpenCoursesHeader td {} to style.

the class OpenCoursesHeader only seems to be applied to the first table.

Any ideas how I get it to apply to the second? There will always only be two tables on the page if that makes it simpler.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

6 Answers6

3

You could use the first-child selector. first will only select the first element of the selector you're working with.

$(function() {
 $(".OpenCourse tr:first-child").addClass("OpenCoursesHeader");
});

But if you have nested tables, this solution will also add the class to the first tr.

VVV
  • 7,563
  • 3
  • 34
  • 55
  • Still suffers from the same issue as mine when multiple `thead`/`tbody`s are concerned - [fiddle](http://jsfiddle.net/ult_combo/bUqPX/3/), in which case I'd prefer Vision's solution. – Fabrício Matté Oct 17 '12 at 16:16
  • @FabrícioMatté True. I've pointed out the problem with nested tables but didn't think about thead/tbody. Good point! – VVV Oct 17 '12 at 16:19
  • If I remember correctly, `:first` isn't necessarily the first on the page, just the first in whatever set is currently being worked with (in this case all `tr` children of `.OpenCourse` elements) – nkorth Oct 17 '12 at 16:57
1

You may use find() and it will work:

$(".OpenCourse").find("tr:first").addClass("OpenCoursesHeader");

DEMO: http://jsfiddle.net/bUqPX/1/ -- thanks to Fabrício :)

Community
  • 1
  • 1
VisioN
  • 143,310
  • 32
  • 282
  • 281
1
$(function() {
    $(".OpenCourse tr:nth-child(1)").addClass("OpenCoursesHeader");
});

Fiddle

:nth-child() selector Reference

Or you can use the :first-child selector as answered by @ComputerArts, which is equivalent to :nth-child(1) as per documentation.

Also, this may not work properly if you have <thead>/<tbody>/<tfooter> elements, in that case you must specify the one you want to apply the styling to in the selector as well:

$(".OpenCourse thead:nth-child(1) tr:nth-child(1)").addClass("OpenCoursesHeader");
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
0

Try this:

$(function() {  
     $("table tr:first").addClass("OpenCoursesHeader"); 
});

This will apply to all tables on the page. Hope this Helps!!

Praveen
  • 1,449
  • 16
  • 25
0
$(function() {
    $(".OpenCourse").each(function() {
        $("tr:first",this).addClass("OpenCoursesHeader");
    });
});

Give that a try, is there a page with the code on that we can look at?

Selven
  • 276
  • 4
  • 12
0

Select table first and do a find on tr:first.

Below is assuming that OpenCourse is a container for collection of tables..

$(".OpenCourse table").find("tr:first").addClass("OpenCoursesHeader");

If OpenCourse is the class of the table then try @Vision's answer

Community
  • 1
  • 1
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134