1
我想使用innerHTML來顯示一個變量,它通過json文件中的一個循環運行並添加到自身,但是當它與innerHTML一起添加時,它顯示爲0。 .log以確保它按照它應該的方式添加數字,而且它確實是這樣,所以我不太清楚問題所在(對我來說簡單易行,我今天從字面上開始使用Javascript)。
CONSOLE.LOG:http://i.imgur.com/cPaCMJD.png
它的外觀在網頁:http://i.imgur.com/BHld58C.png
我的javascript:Javascript innerHTML顯示變量爲0
window.onload = function() {
//Creates variable count
var count = 0;
//Runs through json file in a loop so the count can find the object amount
$.getJSON("/quake/quake.txt", function(json1) {
$.each(json1, function(key, data) {
count = count + 1;
count = count - (Number(data.mag) < 3.1);
console.log(count);
});
});
//Displays count in element with ID quakecount
document.getElementById("quakecount").innerHTML += "Magnitude 3.1+ Earthquakes Displayed: " + count;
}
謝謝,這工作。 – unironicallyironic