简体   繁体   中英

Get a specific column in a table

What I want is to get a specific table column using jquery, so far what I have is this, that selects the first column:

table.find(tr > td:first-child)

But I want to be able to select any column so I can copy it to another table, is there a way to do this for example :

td:n-child 

so I can send it the number of column and get all the data from that specific column.

table.find("tr > td").eq(n);

我只是从心底写这个,所以我无法确认它是否有效,但是我认为这是执行此操作的语法。

:eq() Selector : Description: Select the element at index n within the matched set.

You could use :eq() Selector , eg :

$('tr > td:eq(n)')

Hope this helps.

 $('td:eq(2)').css('background-color','green') $('tr:eq(2) td:eq(0)').css('background-color','red') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border=1> <tr> <td>1</td> <td>A1</td> <td>B1</td> </tr> <tr> <td>2</td> <td>A2</td> <td>B2</td> </tr> <tr> <td>3</td> <td>A3</td> <td>B3</td> </tr> </table> 

Try this:

for instance for selecting the 2nd element, you would:

table.find("tr > td:nth-child(2)");

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