在没有setInterval的情况下检测类更改 [英] Detecting class change without setInterval
本文介绍了在没有setInterval的情况下检测类更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个div
,它以编程方式添加了其他类.不使用此setInterval
实现,如何检测类名更改?
I have a div
that has additional classes added to it programmatically. How can I detect the class name change without using this setInterval
implementation?
setInterval(function() {
var elem = document.getElementsByClassName('original')[0];
if (elem.classList.contains("added")) { detected(); }
}, 5500);
MutationObserver?
MutationObserver?
推荐答案
var e = document.getElementById('test')
var observer = new MutationObserver(function (event) {
console.log(event)
})
observer.observe(e, {
attributes: true,
attributeFilter: ['class'],
childList: false,
characterData: false
})
setTimeout(function () {
e.className = 'hello'
}, 1000)
<div id="test">
</div>
这篇关于在没有setInterval的情况下检测类更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文