I have a div and it has several input elements in it... I'd like to iterate through each of those elements. Ideas?
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
Use
children()
andeach()
, you can optionally pass a selector tochildren
You could also just use the immediate child selector:
I don't think that you need to use
each()
, you can use standard for loopthis way you can have the standard for loop features like
break
andcontinue
works by defaultalso, the debugging will be easier
It is also possible to iterate through all elements within a specific context, no mattter how deeply nested they are:
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.
children() is a loop in itself.
It can be done that way as well.
$('input', '#div').each(function () { console.log($(this)); //log every element found to console output });
If you need to loop through child elements recursively: