开发者

Selecting only nth-child of a certain element type

I have a several forms, and I'd like to select only the first and second input box of each form

$("form input:nth-child(1), form input:nth-child(2)");
开发者_运维知识库

But this doesn't work because each input has a label next to it, so there is no input that is nth-child(1).

Example at jsbin


You can do it using :lt(), like this:

$("form input:lt(2)");

This selects all elements that match at less-than the passed index, the first and second elements are index 0 and 1, and will match this selector :)


You can use eq():

$("form input:eq(0), form input:eq(1)");

This selects the first and second of the matched elements.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜