使用jQuery选择第n列中的所有单元格

Mul*_*ner 4 javascript jquery css-selectors jquery-selectors

如何选择普通html表的第n列中的所有单元格.我试过这个,但它不起作用:

    $('table#foo tbody td:nth-child(3)').each(function (index) {
        $(this).addClass('hover');
    });
Run Code Online (Sandbox Code Playgroud)

更新:继承人的工作代码:http://jsfiddle.net/Claudius/D5KMq/

fea*_*net 10

没有必要使用each它.

$('table#foo tbody td:nth-child(3)').addClass('hover');
Run Code Online (Sandbox Code Playgroud)

除此之外,您的代码没有任何问题.问题必须在其他地方.


Aln*_*tak 6

您的实际问题(在原始问题中不明显,但在小提琴中显而易见)是.index()返回从零开始的值,但:nth-child()需要基于1的值.