Подтвердить что ты не робот

Как выбрать столбец таблицы с помощью jQuery

Я хочу выбрать столбец таблицы, и все, что я знаю, это текст заголовка столбца. (Th.innerText)

Я попробовал следующий код, но он не работает:

ownerIndex = $('th:contains("Owner")').index();
$('table tr td:nth-child(ownerIndex)')

любые идеи?

4b9b3361

Ответ 1

Ok. Я нашел решение:

$('table tr td:nth-child('+ownerIndex+')')

Ответ 2

В приведенном выше примере ownerIndex необходимо увеличить на 1 для соответствия индексированию nth-child, который начинается с 1, а не из 0.

Вот мой прием: http://jsfiddle.net/2xU8t/

/* Set all the cells in columns with THEHEADING in the heading to red */

// Find the heading with the text THEHEADING
columnTh = $("table th:contains('THEHEADING')");

// Get the index & increment by 1 to match nth-child indexing
columnIndex = columnTh.index() + 1; 

// Set all the elements with that index in a tr red
$('table tr td:nth-child(' + columnIndex + ')').css("color", "#F00");

// Set the heading red too!
columnTh.css("color", "#F00");