开发者

JQuery - How to select :odd immediate children of an element

I have a table as this:

<table class='table'>
<tr></tr>
<tr><tr>
<tr>
<table><tr></tr></table
<tr></tr>
</table>

and I want to select only the开发者_Python百科 immediate odd tr after .table.

What I tried is:

$('.table > tr:odd').addClass('odd');

But it doesn't do the job.

Anyone knows? Thanks.


and I want to select only the immediate tr after .table.

You are using different class name than you mention, try:

 $('.table > tr:odd').addClass('odd');

Also make sure to wrap your code in ready handler:

$(function(){
     $('.table > tr:odd').addClass('odd');
});

You should also try adding tbody:

$('.table > tbody > tr:odd').addClass('odd');


A different option will be to use the filter()(docs) method.

$('.table > tbody > tr').filter(function(i){
    return !(i % 2);
}).addClass('odd');

This will enable you to use a valid CSS selector in your initial selection giving a good performance boost in browsers that support querySelectorAll.

Example: http://jsfiddle.net/kbnH2/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜