¿Cómo esperar hasta que exista un elemento?
Estoy trabajando en una extensión en Chrome, y me pregunto: ¿cuál es la mejor manera de saber cuándo un elemento entra en existencia? Usando javascript simple, con un intervalo que comprueba hasta que un elemento existe, o jQuery tiene alguna manera fácil de hacer esto?
175
1 answers
DOMNodeInserted
está en desuso, junto con los otros eventos de mutación DOM, debido a problemas de rendimiento - el enfoque recomendado es usar un MutationObserver para ver el DOM. Sin embargo, solo es compatible con navegadores más nuevos, por lo que debe volver a DOMNodeInserted
cuando MutationObserver
no esté disponible.
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (!mutation.addedNodes) return
for (var i = 0; i < mutation.addedNodes.length; i++) {
// do things to your newly added nodes here
var node = mutation.addedNodes[i]
}
})
})
observer.observe(document.body, {
childList: true
, subtree: true
, attributes: false
, characterData: false
})
// stop watching using:
observer.disconnect()
113
Author: hughsk,
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-08-05 12:39:58
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-08-05 12:39:58