如何检查 DOM 中的值是否发生变化?我正在调用 Ajax 函数从数据库中提取一些数据。如果已推送新值,我想对该特定 DIV 闪烁或做一些动画。因此,当“可用性”更改时,动画只会执行一次。仅到值已更改的 DIV。
function getRealData() {
$.ajax({
url: 'test_api.php',
data: "",
dataType: 'json',
success: function (rows) {
var text = '';
for (var i in rows) {
var row = rows[i];
var availability = row[3];
var hostName = row[2];
var intranet = row[6];
var timeRespons = row[4];
//console.log(availability);
if (availability == 0){
var img = "img/tick.png";
}
if (availability == 1){
var img = "img/warning.png";
}
if (availability == 2){
var img = "img/alert.png";
}
text+= '<section class="square"><h3> ' + intranet + '</h3><img src='+img+' width="70" height="70" rel='+availability+' alt="warning"/><h4><img src="img/time_icon.png" width="20" height="20" alt="clock" class="clock" /> '+ timeRespons+'</h4>';
text += '</section>';
}
$("#journey").html(text);
}
});
}
setInterval(getRealData, 2000); //this refreshes data every 2seconds
getRealData(); //call the function to display data
输出是:
<div id="journey">
<div class="square>availability: 0 hostName: aaa intranet: ttt timeResponse:0.124</div>
<div class="square>availability: 0 hostName: qqq intranet: hhh timeResponse:0.064</div>
<div class="square>availability: 0 hostName: www intranet: nnn timeResponse:0.303</div>
<div class="square>availability: 0 hostName: rrr intranet: rrr timeResponse:0.019</div>
<div class="square>availability: 0 hostName: eee intranet: uuu timeResponse:0.025</div>
<div class="square>availability: 0 hostName: ggg intranet: ooo timeResponse:0.158</div>
</div>