jQuery select child element by class with unknown path
Estoy tratando de averiguar la sintaxis para seleccionar un n-ésimo hijo de un elemento por su clase, sin embargo, no conozco la ruta exacta al elemento. No puedo hacer $('parent > child > grandchild > hereIam');
Así que básicamente necesito ser capaz de decir
$('#thisElement').AllRelativesWithClass('.classToSelect')
¿Cómo hago eso exactamente?
26
4 answers
De acuerdo con esta documentación , el método find buscará a través del árbol de elementos hasta encontrar el elemento en los parámetros del selector. Así que $(parentSelector).find(childSelector)
es la forma más rápida y eficiente de hacer esto.
35
Author: Casey,
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
2015-09-08 21:17:08
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
2015-09-08 21:17:08
$('#thisElement').find('.classToSelect')
encontrará cualquier descendiente de #thisElement
con clase classToSelect
.
19
Author: cfs,
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
2013-08-02 12:26:31
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
2013-08-02 12:26:31
Esto debería hacer el truco:
$('#thisElement').find('.classToSelect')
12
Author: Joe Meyer,
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
2013-08-02 12:24:46
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
2013-08-02 12:24:46
Prueba esto
$('#thisElement .classToSelect').each(function(i){
// do stuff
});
Espero que ayude
5
Author: Sonu Sindhu,
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
2013-08-02 12:27:23
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
2013-08-02 12:27:23