文章
observer监听html元素变化
时间:2024-12-17 11:42
阅读:568
<html>
<head>
<script src="..\Desktop\ys_faceposter\statics\js\jquery.min.js" ></script>
</head>
<body>
<img src="photo.jpg" id="img" />
<button id="change">更换</button>
<button id="change_height">变小</button>
</body>
<script>
$("#change_height").click(function(){
$("#img").attr('width','100px');
});
//点击更换
$("#change").click(function(){
$("#img").attr('src','prize.jpg');
});
//监听
var img = $("#img")[0];
console.log(img);
var observer = new MutationObserver(function (mutations, observer) {
mutations.forEach(function(mutation) {
setsrc();
});
});
var options = {
attributes:true,
attributeFilter:['src']
} ;
var setsrc = function(){
alert($("#img").attr("src"));
}
observer.observe(img, options);
</script>
</html>