“Cómo escribir un observador de mutaciones js” Código de respuesta

Observador de mutaciones JS

const observer = new MutationObserver(list => {
    console.log('mutation list', list);
});
observer.observe(document.body, {
    attributes: true,
    childList: true,
    subtree: true
});
// perform any DOM change action in your page. e.g. show/hide/remove
Anthony Smith

Cómo escribir un observador de mutaciones js

let mList = document.getElementById('myList'),
            options = {
                childList: true,
                attributes: true,
                subtree: true
            },
            observer = new MutationObserver(mCallback);

        function mCallback(mutations) {
            for (let mutation of mutations) {
                // If you remove a child from the container you are watching
                if (mutation.type === 'childList') {
                    console.log('Mutation Detected: A child node has been added or removed.');
                }
                // If you use setAttribute to add a class or ID to an element
                if (mutation.type === 'attributes') {
                    console.log('Mutation Detected: An attribute has been added or removed.');
                }

                if (mutation.type === 'subtree') {
                    console.log('Mutation Detected: A subtree has been added or removed.');
                }
            }
        }

observer.observe(mList, options);
Anthony Smith

Respuestas similares a “Cómo escribir un observador de mutaciones js”

Preguntas similares a “Cómo escribir un observador de mutaciones js”

Más respuestas relacionadas con “Cómo escribir un observador de mutaciones js” en JavaScript

Explore las respuestas de código populares por idioma