admin管理员组

文章数量:1023764

Given a 3 by 3 table, i want to add a class to all the cells of 3rd column .

I have tried doing

$( 'td:eq(3)' ).addclass('special');
$( 'td:eq(5)' ).addclass('special');
$( 'td:eq(8)' ).addclass('special');

but the problem is writing 3 lines of code. Can a single line of code do it ?

Given a 3 by 3 table, i want to add a class to all the cells of 3rd column .

I have tried doing

$( 'td:eq(3)' ).addclass('special');
$( 'td:eq(5)' ).addclass('special');
$( 'td:eq(8)' ).addclass('special');

but the problem is writing 3 lines of code. Can a single line of code do it ?

Share Improve this question asked Feb 11, 2012 at 15:50 HalfWebDevHalfWebDev 7,66814 gold badges71 silver badges110 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7
$('tr > td:nth-child(3)').addClass('special');

DEMO: http://jsfiddle/TcQex/

DOCS: http://api.jquery./nth-child-selector

$("td:nth-child(3)").addClass('special');

good article about nth-child -

http://css-tricks./how-nth-child-works/

Given a 3 by 3 table, i want to add a class to all the cells of 3rd column .

I have tried doing

$( 'td:eq(3)' ).addclass('special');
$( 'td:eq(5)' ).addclass('special');
$( 'td:eq(8)' ).addclass('special');

but the problem is writing 3 lines of code. Can a single line of code do it ?

Given a 3 by 3 table, i want to add a class to all the cells of 3rd column .

I have tried doing

$( 'td:eq(3)' ).addclass('special');
$( 'td:eq(5)' ).addclass('special');
$( 'td:eq(8)' ).addclass('special');

but the problem is writing 3 lines of code. Can a single line of code do it ?

Share Improve this question asked Feb 11, 2012 at 15:50 HalfWebDevHalfWebDev 7,66814 gold badges71 silver badges110 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7
$('tr > td:nth-child(3)').addClass('special');

DEMO: http://jsfiddle/TcQex/

DOCS: http://api.jquery./nth-child-selector

$("td:nth-child(3)").addClass('special');

good article about nth-child -

http://css-tricks./how-nth-child-works/

本文标签: javascriptTraversing a table column jQueryStack Overflow