📜  如何编写变异观察者 js - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:18.858000             🧑  作者: Mango

代码示例2
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);