<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>监听dom节点的变化</title>
</head>
<body>
<div id="div" class="class1">监听dom节点class的变化</div>
<script>
var target=document.getElementById('div')
var options={
attributes:true,//观察node对象的属性
attributeFilter:['class']//只观察class属性
}
var mb=new MutationObserver(function(mutationRecord,observer){
console.log(mutationRecord)
console.log(observer)
})
mb.observe(target,options)
var i=0;
setInterval(function(){
target.classList.add('class'+i++)
},1000)
</script>
</body>
</html>
网友评论