我希望在另一个javascript文件从中删除特定类之后更改项目的文本。如果我无法访问正在删除类的javascript,如何在类删除时触发事件?
答案 0 :(得分:0)
你可以使用变异观察者:
let observer = new MutationObserver(function(mutationsList) {
for (let mutation of mutationsList) {
// this if is necessary if you want to observe a lot of different things by one observer
if (mutation.type == "attributes" && mutation.target == elementYouWantToObserveClassChanges && mutation.attributeName == "class") {
// mutation.oldValue now contains old class attribute
// current classes can be checked in normal way using .className or .classList properties)
}
}
});
observer.observe(elementYouWantToObserveClassChanges, {attributes: true, attributeFilter: ['class']});
代码基于https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
的示例