• 监听节点属性变化


    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            * {
                font-size: 28px;
            }
        </style>
    </head>
    
    <body>
    
        <div id="container">
    
        </div>
    
        <div>
            <button type="button" id="btnAdd">添加子节点</button>
        </div>
    
    
        <script>
            const containerEl = document.getElementById('container');
            // 观察器的配置(需要观察什么变动)
            const config = { attributes: true, childList: true, subtree: true };
    
            // 当观察到变动时执行的回调函数
            const callback = function (mutationsList, observer) {
                // Use traditional 'for loops' for IE 11
                for (let mutation of mutationsList) {
                    if (mutation.type === 'childList') {
                        console.log(`A child node has been added or removed. ${performance.now()}`);
                    }
                    else if (mutation.type === 'attributes') {
                        console.log('The ' + mutation.attributeName + ' attribute was modified.');
                    }
                }
            };
            // 创建一个观察器实例并传入回调函数
            const observer = new MutationObserver(callback);
            // 以上述配置开始观察目标节点
            observer.observe(containerEl, config);
    
            btnAdd.onclick = function () {
                setTimeout(function () {
                    console.log("setTimeout callback:", performance.now());
                });
                containerEl.append(`added node: ${performance.now()}`);
            }
    
    
        </script>
    
    </body>
    
    </html>
  • 相关阅读:
    java 集合框架(十五)Deque
    java 集合框架(十四)Queue
    java 集合框架(十)List
    java 集合框架(四)Set
    java 集合框架(三)Collection
    java 集合框架(一)概述
    java 集合框架(二)Iterable接口
    java I/O框架 (四)文件流
    全零网络IP地址0.0.0.0表示意义详谈
    windows在cmd执行svn 命令
  • 原文地址:https://www.cnblogs.com/angdh/p/16556100.html
Copyright © 2020-2023  润新知