jQuery:first和:last selector

时间:2011-03-22 19:08:48

标签: jquery

$('div#nutrition-fact-content table tr:first td:first').addClass('product-volume');
$('div#nutrition-fact-content table tr:first td:last').addClass('flavor-name');

在上面的代码中,jQuery的第二行应用了这个类。它之前的td从未收到过课程。我也试过tr:首先对行进行分类,这也没有得到一个类。为什么不呢?

2 个答案:

答案 0 :(得分:1)

试试这个:

$('div#nutrition-fact-content table tr:first td').first().addClass('product-volume');
$('div#nutrition-fact-content table tr:first td').last().addClass('flavor-name');

更新2
如果你没有jQuery> 1.4.4: 看看这个fiddle

像魅力一样: - )

$children = $('div#nutrition-fact-content table tr:first').children().length -1;

$($('div#nutrition-fact-content table tr:first').children()[0]).addClass('product-volume');
$($('div#nutrition-fact-content table tr:first').children()[$children]).addClass('flavor-name');

答案 1 :(得分:0)

选择器似乎在我测试here时起作用。 你确定其他一切都好吗?

相关问题