如何使用 jQuery 迭代 div 的子元素?

发布于 2024-09-04 22:02:38 字数 42 浏览 5 评论 0原文

我有一个 div,里面有几个输入元素...我想迭代每个元素。有想法吗?

I have a div and it has several input elements in it... I'd like to iterate through each of those elements. Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

如痴如狂 2024-09-11 22:02:38

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

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

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

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

Use children() and each(), you can optionally pass a selector to children

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

You could also just use the immediate child selector:

$('#mydiv > input').each(function () { /* ... */ });
春风十里 2024-09-11 22:02:38

还可以迭代特定上下文中的所有元素,无论它们嵌套的深度如何:

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

传递给 jQuery 'input' 选择器的第二个参数 $('#mydiv') 是上下文。在这种情况下,each() 子句将迭代 #mydiv 容器内的所有输入元素,即使它们不是 #mydiv 的直接子元素。

It is also possible to iterate through all elements within a specific context, no mattter how deeply nested they are:

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

The second parameter $('#mydiv') which is passed to the jQuery 'input' Selector is the context. In this case the each() clause will iterate through all input elements within the #mydiv container, even if they are not direct children of #mydiv.

征﹌骨岁月お 2024-09-11 22:02:38

如果您需要递归地循环子元素:

function recursiveEach($element){
    $element.children().each(function () {
        var $currentElement = $(this);
        // Show element
        console.info($currentElement);
        // Show events handlers of current element
        console.info($currentElement.data('events'));
        // Loop her children
        recursiveEach($currentElement);
    });
}

// Parent div
recursiveEach($("#div"));   

注意:
在此示例中,我显示了向对象注册的事件处理程序。

If you need to loop through child elements recursively:

function recursiveEach($element){
    $element.children().each(function () {
        var $currentElement = $(this);
        // Show element
        console.info($currentElement);
        // Show events handlers of current element
        console.info($currentElement.data('events'));
        // Loop her children
        recursiveEach($currentElement);
    });
}

// Parent div
recursiveEach($("#div"));   

NOTE:
In this example I show the events handlers registered with an object.

恋你朝朝暮暮 2024-09-11 22:02:38
$('#myDiv').children().each( (index, element) => {
    console.log(index);     // children's index
    console.log(element);   // children's element
 });

这将遍历所有子元素,并且可以分别使用 elementindex 分别访问具有索引值的子元素。

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

This iterates through all the children and their element with index value can be accessed separately using element and index respectively.

孤独患者 2024-09-11 22:02:38

也可以这样完成:

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

It can be done this way as well:

$('input', '#div').each(function () {
    console.log($(this)); //log every element found to console output
});
仙女 2024-09-11 22:02:38

我认为您不需要使用 each(),您可以使用标准的 for 循环

var children = $element.children().not(".pb-sortable-placeholder");
for (var i = 0; i < children.length; i++) {
    var currentChild = children.eq(i);
    // whatever logic you want
    var oldPosition = currentChild.data("position");
}

,这样您就可以拥有标准的 for 循环功能,例如 breakcontinue 默认情况下

也可以工作,调试会更容易

I don't think that you need to use each(), you can use standard for loop

var children = $element.children().not(".pb-sortable-placeholder");
for (var i = 0; i < children.length; i++) {
    var currentChild = children.eq(i);
    // whatever logic you want
    var oldPosition = currentChild.data("position");
}

this way you can have the standard for loop features like break and continue works by default

also, the debugging will be easier

浪菊怪哟 2024-09-11 22:02:38

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

$('.element').children().animate({
'opacity':'0'
});

children() is a loop in itself.

$('.element').children().animate({
'opacity':'0'
});
情话难免假 2024-09-11 22:02:38

它与 .attr('value') 一起使用,用于元素属性

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

It's working with .attr('value'), for elements attributes

$("#element div").each(function() {
   $(this).attr('value')
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文