How do I iterate through children elements of a div using jQuery?

后端 未结 7 1330
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 01:28

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

相关标签:
7条回答
  • 2020-11-28 02:05

    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.

    0 讨论(0)
提交回复
热议问题