通常在编写文章时添加此配置,可以根据文章标签的变化实时生成文章目录,方便把握页面整体结构。
x// 选择需要观察变动的节点
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();
1、MutationObserver 所观察的DOM变动(即上面代码的option对象),包含以下类型:
想要观察哪一种变动类型,就在option对象中指定它的值为true。
需要注意的是,不能单独观察subtree变动,必须同时指定childList、attributes和characterData中的一种或多种。
除了变动类型,option对象还可以设定以下属性:
2、disconnect方法用来停止观察。发生相应变动时,不再调用回调函数。
xxxxxxxxxx
mo.disconnect();
3、takeRecord方法用来清除变动记录,即不再处理未处理的变动。
xxxxxxxxxx
mo.takeRecord();
DOM对象每次发生变化,就会生成一条变动记录。这个变动记录对应一个MutationRecord对象,该对象包含了与变动相关的所有信息。Mutation Observer 进行处理的一个个变动对象所组成的数组。
MutationRecord对象包含了DOM的相关信息,有如下属性:
xxxxxxxxxx
function setCatalogue() {
let target = document.getElementsByClassName("ck-content")[0];
let h2nbsp = "";
let h3nbsp = " ";
let h4nbsp = " ";
$(".right-navigate-list").empty();
$(target).children().each(function (index, element) {
let tagName = $(this).get(0).tagName;
if (tagName.substr(0, 1).toUpperCase() === "H") {
let contentH = $(this).html();
let markid = "mark-" + tagName + "-" + index.toString();
$(this).attr("id", markid);
let nbsp = "";
if (tagName.indexOf("3") !== -1) {
nbsp = h3nbsp;
} else if (tagName.indexOf("4") !== -1) {
nbsp = h4nbsp;
}
$(".right-navigate-list").append("<li><a href='#" + markid + "' title='" + contentH + "' target='_self'><span>" + nbsp + "</span><i class='bi-arrow-bar-right' aria-hidden='true'></i>" + contentH + "</a></li>");
}
});
}
window.onload = function (){
setCatalogue()
let callback = function(records) {
setCatalogue();
// records.map(function(record) {
// console.log('mutation type:', record.type);
// console.log('mutation target:', record.target);
// });
};
let mo = new MutationObserver(callback);
let options = {
childList: true,
subtree: true
};
let target = document.getElementsByClassName("ck-content")[0];
mo.observe(target, options);
};
xxxxxxxxxx
实例1 - 子元素的变动
var callback = function(records) {
records.map(function(record) {
console.log('mutation type:', record.type);
console.log('mutation target:', record.target);
});
};
var mo = new MutationObserver(callback);
var options = {
childList: true,
subtree: true
};
// 观察body元素的所有下级元素(childList表示观察子元素,subtree表示观察子元素的下级元素)的变动。回调函数会在控制台显示所有变动的类型和目标元素。
mo.observe(document.body, options);
实例2 - 属性变动
var callback = function(records) {
records.map(function(record) {
console.log('previous attribute:', record.oldValue);
});
};
var mo = new MutationObserver(callback);
var element = document.querySelector('#app');
var options = {
attribute: true,
attributeOldValue: true
};
mo.observe(element, options);