¿Cómo iterar a través de elementos secundarios de un div usando jQuery?


Tengo un div y tiene varios elementos de entrada en él... Me gustaría recorrer cada uno de esos elementos. ¿Ideas?

Author: Shamoon, 2010-06-11

5 answers

Uso children() y each(), opcionalmente, se puede pasar un selector children

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

También podría usar el selector secundario inmediato:

$('#mydiv > input').each(function () { /* ... */ });
 395
Author: Andy E,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-22 08:31:54

También es posible iterar a través de todos los elementos dentro de un contexto específico, sin importar cuán profundamente anidados estén:

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

El segundo parámetro $('#mydiv') que se pasa al Selector 'input' de jQuery es el contexto. En este caso, la cláusula each () iterará a través de todos los elementos de entrada dentro del contenedor #mydiv, incluso si no son hijos directos de #mydiv.

 43
Author: Liquinaut,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-07 15:29:36

Si necesita recorrer elementos secundarios recursivamente :

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"));   

NOTA: En este ejemplo muestro los controladores de eventos registrados con un objeto.

 3
Author: tomloprod,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-30 07:55:32

También se puede hacer de esa manera.
$('input', '#div').each(function () { console.log($(this)); //log every element found to console output });

 2
Author: Umar Asghar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-08 14:26:24

Children() es un bucle en sí mismo.

$('.element').children().animate({
'opacity':'0'
});
 1
Author: Dan185,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-26 17:01:23