我有以下 HTML:
<input id="1" type="checkbox"/>
<label for="1">bla bla</label>
<br/>
<input id="2" type="checkbox"/>
<label for="2">hello hello</label>
<br/>
.
.
.
etc...
有没有办法在一次调用中选择前几个输入?换句话说,我不想全选它们并循环遍历每一个。只需选择 1 条语句,例如前 3 条。
我有以下 HTML:
<input id="1" type="checkbox"/>
<label for="1">bla bla</label>
<br/>
<input id="2" type="checkbox"/>
<label for="2">hello hello</label>
<br/>
.
.
.
etc...
有没有办法在一次调用中选择前几个输入?换句话说,我不想全选它们并循环遍历每一个。只需选择 1 条语句,例如前 3 条。
你的意思是这样吗?
var firstThree = $('#1, #2, #3')
这应该这样做
$("input:lt(3)")
jQuery 函数slice允许您仅指定结果的子集:
$('input').slice(0, 3)
Where 0 is the first index and 3 is the first index not included, so 2 is the last index and you get the three items.
干得好:
$('input').filter(function(index) {
return index < 3;
});
您可以将其更改为3
您想要的任意数量的项目。
您可以使用 jQuery:gt
和:lt
选择器:
$('input:lt(3)').css('blah, 'blah');
这应该选择页面上的前三个输入标签。
您可以使用:lt
选择器。