我有一个div,它有几个输入元素在它…我想要遍历每一个元素。想法吗?


当前回答

它使用.attr('value')来处理元素属性

$("#element div").each(function() {
   $(this).attr('value')
});

其他回答

它使用.attr('value')来处理元素属性

$("#element div").each(function() {
   $(this).attr('value')
});

也可以这样做:

$('input', '#div').each(function () {
    console.log($(this)); //log every element found to console output
});

使用children()和each(),您可以选择将选择器传递给子代

$('#mydiv').children('input').each(function () {
    alert(this.value); // "this" is the current element in the loop
});

你也可以只使用直接子选择器:

$('#mydiv > input').each(function () { /* ... */ });

Children()本身就是一个循环。

$('.element').children().animate({
'opacity':'0'
});
$('#myDiv').children().each( (index, element) => {
    console.log(index);     // children's index
    console.log(element);   // children's element
 });

这个迭代遍历所有的子元素,它们的带有index值的元素可以分别使用element和index访问。