我有4个带有表单字段的字段集。
如何选择每个字段集中的每个第一个标签标签并将其包含在变量中?
这就是我的尝试:
var $firstLabel = $('fieldset label');
然后我称之为:
$firstLabel.eq(0)
这并不完全符合我的工作方式,因为它只选择第一个字段集中的第一个标签,而不是每个字段集中的每个第一个标签。
感谢。
答案 0 :(得分:2)
如果您使用map()
执行了以下操作。
var labels = $('fieldset').map(function () {
return $(this).find('label:first');
});
labels
变量将是4个标签的jQuery集合,因此您可以使用each()
迭代它们;
labels.each(function (i) {
// this function will get called 4 times; each time `this` will be one of the labels.
});
甚至将它们分配给变量;
var firstLabel = labels.eq(0);
var secondLabel = labels.eq(1);
......等等。