3 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
.load()加載后,您可以使用回調(diào)函數(shù)來檢查 DIV 的文本。使用.text()來獲取DIV的內(nèi)容。
$("#last_relay1").load('last_update.php #RELAY1_STATUS_last_update', function() {
if ($this).text().trim() == "0") {
$("#image").prop("src", "imageA.png");
} else {
$("#image").prop("src", "imageB.png");
}
});

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
我解決了。我遵循 Barmar 提供的提示。
我使用了以下代碼...
var value = $("#last_relay1").load('last_update.php #RELAY1_STATUS_last_update', function() {
var value_to_test = value.text();
if(value_to_test == 1){
$("#last_relay1").empty();
$('<img src="img/ON.png">').appendTo("#last_relay1"); }
else {
$('<img src="img/OFF.png">').appendTo("#last_relay1");
}
});

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
我認(rèn)為您要做的是將數(shù)據(jù)從網(wǎng)頁(由 JavaScript 生成)發(fā)送到 PHP 以決定提供哪個(gè)圖像。
您提出問題的方式不會(huì)幫助您找到解決方案(http://xyproblem.info/)。
相反,您需要:
預(yù)加載圖像 A 和圖像 b,并通過取消隱藏在 javascript 中顯示您想要的圖像。
通過發(fā)出返回正確圖像的XHR 請(qǐng)求,使用 PHP動(dòng)態(tài)加載圖像。
- 3 回答
- 0 關(guān)注
- 208 瀏覽
添加回答
舉報(bào)