[英]Real Time DIV background color update based on database value
我希望div根據從數據庫檢索的值來更改顏色。 當前代碼不提供任何顏色,而是使用設置的CSS顏色。
主div CSS
.status{ background: #000; }
status.php
if ($user_data['status'] === "1") { echo "online"; }
else { echo "offline"; }
Ajax功能
$(document).ready(function() {
setInterval(function(){
$.ajax({
url: 'status.php',
type: 'GET',
success: function(data) {
if (data === "online") {
$('.status').css('background', '#40A547');
} else {
$('.status').css('background', '#7f8c8d');
}
}
});
}, 2000);
});
主要div
<div class="status">something</div>
請幫幫我。 所有幫助將不勝感激!
可以使用以下代碼識別問題:
$(document).ready(function() {
setInterval(function(){
$.ajax({
url:'status.php',
datatype:"application/json",
type:'GET',
success:function(data){
if (data === "online") {
$('.status').css('background', '#40A547');
} else {
$('.status').css('background', '#7f8c8d');
}
},
error:function(){
//if pointer is comming it means there is some error in the back end code
}
});
}, 2000);
});
現在在firebug
檢查您的error details
,它將顯示error details
,如果您沒有firebug,請添加它,然后進行檢查。
還將一些content in the div
以便可見。 如果沒有內容,則div不會出現。
希望這會有所幫助!
您的div中沒有任何內容,因此高度和寬度基本上為0,因此您看不到任何內容(這就是我所假設的意思,“當前代碼不提供任何顏色”)。
如果您指定高度和寬度,或在div內添加某些內容(甚至是
您也應該會看到背景色)。
這意味着您不會從ajax回來“在線”。 您需要編輯要進行AJAXing處理的PHP代碼,以純粹說出“在線”,沒有div或標記。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.