赞
踩
- // 当观察到变动时执行的回调函数
- const callback = function(mutationsList) {
- for(let mutation of mutationsList) {
- if (mutation.type === 'childList') {
- console.log('TODO someting');
- } else if(mutation.type === 'attributes') {
- console.log('TODO someting');
- }
- }
- }
-
- // 创建一个观察器实例并传入回调函数
- const observer = new MutationObserver(callback);
-
- // 选择需要观察变动的节点
- const targetNode = document.body;
-
- // 观察器的配置(需要观察什么变动)
- const config = { attributes: true, childList: true, subtree: true, characterData: true, };
-
- // 以上述配置开始观察目标节点
- observer.observe(targetNode, config);
-
- // 之后,可停止观察
- observer.disconnect();

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。