Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第471天 说说MutationObserver的应用场景有哪些? #2710

Open
haizhilin2013 opened this issue Jul 29, 2020 · 2 comments
Open
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第471天 说说MutationObserver的应用场景有哪些?

3+1官网

我也要出题

@haizhilin2013 haizhilin2013 added the js JavaScript label Jul 29, 2020
@zhaofeipeter
Copy link

zhaofeipeter commented Aug 1, 2020

MutationObserver用于监视Dom节点的变动情况,例:

// 选择需要观察变动的节点
const targetNode = document.getElementById('some-id');
// 观察器的配置(需要观察什么变动)
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.');
}
else if (mutation.type === 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
}
};
// 创建一个观察器实例并传入回调函数
const observer = new MutationObserver(callback);

// 以上述配置开始观察目标节点
observer.observe(targetNode, config);

// 之后,可停止观察
observer.disconnect();

@zhizhiyaya
Copy link

性能统计、白屏统计、页面元素展示 自动化 埋点

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

3 participants