最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Traversing a table column -jQuery - Stack Overflow

matteradmin4PV0评论

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/

Post a comment

comment list (0)

  1. No comments so far