假设我有一个 Ajax 函数,它每 5 秒显示一次新数据。现在,每当推送(显示)新数据时,我都想更改该特定值的 css (class="av") 颜色。ajax 当前显示 5 行,但我只想更改该更改值的颜色。我怎么做?非常感谢
我的代码:
<script type="text/javascript">
$(document).ready(function(){
//function to get data from database
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];
text += "<div class='box'><b>availability: </b><span class='av'>"+availability+"</span><b> hostName: </b>"+hostName+
"<b> intranet: </b>"+intranet+"<b> timeResponse: </b>"+timeRespons;
text += '<br/ ></div>';
}
$("#content").html(text);
}
});
}
//this refreshes data every 2seconds
setInterval(getRealData, 5000);
//call the function to display data
getRealData();
//notification
function notify(){
var avValue = $(".av").html();
console.log(avValue);
if(avValue == 1){
$(".av").css({"color":"red"});
}
}
setInterval(notify, 2000);
notify();
});
</script>
编辑:(这是输出,需要更改 1 的颜色)
availability: 1 hostName: aaa intranet: vvvvv timeResponse:0.144
availability: 0 hostName: zzz intranet: rrrrr timeResponse:0.028
availability: 1 hostName: qqq intranet: eeeee timeResponse:0.160
availability: 0 hostName: eee intranet: qqqqq timeResponse:0.116
availability: 0 hostName: ttt intranet: wwwww timeResponse:0.046
availability: 0 hostName: uuu intranet: kkkkk timeResponse:0.116
我还想在 avValue == 1 时向 .box 添加一个新类,然后基于该类是否存在做某事。但这也不起作用。这很奇怪。这可能与异步或缓存ajax有关吗?