开发者

Problem with jQuery's nth-child(n+2) with table structure

The jQuery's nth-child selector isn't working as I expect it to work with the below HTML. It returns 0 instead of (total number SELECTs - 1). It works just fine if I get rid of the TABLE and keep only SELECTs. I noticed that it works fine if I use nth-child(n+1) whether I keep the TABLE structure or not. Moving the alert code to a button click event didn't change anything.

<table>
    <tr>
        <td>
            <select>
                <option value=""></option>
                <option value=""></option>
                <option value=""></option>
            </select>
        </td>
        <td>             
            <select>
                <option开发者_开发百科 value=""></option>
                <option value=""></option>
                <option value=""></option>
            </select>
        </td>
    </tr>
</table>

and jQuery code

$(document).ready(function() {
    alert($("select:nth-child(n+2)").length);
});

jsfiddle demo.

jQuery 1.6.2; IE8 (can't help it!); I'm not using table to create page layout.


If all you want is every select element but the first one then just use select:gt(0)

The reason :nth-child(n+2) doesn't work is because the select elements do not have the same parent. :nth-child depends on counting children of the same parent.

Take a look at this: http://jsfiddle.net/petersendidit/MvkUP/1/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜