我目前正在尝试使用MutationObserver API根据元素的类更改来触发消息(稍后将是函数)。
我目前正在触发有关突变更改的日志,但我假设正在触发诸如 DOM 位置等突变。有没有办法让这种情况下MutationObserver只查找特定的?attributesclass
作为主要的 Drupal 开发人员,我习惯使用 jQuery,这就是为什么我有 classList 函数,因为我喜欢链接。
这是我的代码:
var foo = document.getElementById("foo");
var wrapper = document.getElementById("wrapper");
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.attributeName === "class") {
var attributeValue = mutation.target.getAttribute(mutation.attributeName);
console.log("Class attribute changed to:", attributeValue);
}
});
});
function classList(el) {
var list = el.classList;
return {
add: function(c) {
if (!list.contains(c)){
console.log('Contains: '+list.contains(c));
list.add(c);
}
return this;
},
remove: function(c) {
if (!list.contains(c)){
list.remove(c);
}
return …Run Code Online (Sandbox Code Playgroud)