我想在javascript中聲明存儲在file.txt
中的兩個對象,並在代碼中使用它。如何在JavaScript中使用存儲在我的服務器中的對象?
比如我在index.php
寫了這個:
var counter = Number(localStorage.getItem('rans')) || 0;
var counter1 = Number(localStorage.getItem('bans')) || 0;
$('.redanswer').one('click', function(){
localStorage.setItem('rans', ++counter);
$('.rtotal').text(counter + " concordano");
$('.btotal').text(counter1+ " non concordano");
$('.rgraphic').css("height", counter * 100/(counter1+counter));
$('.bgraphic').css("height", counter1 * 100/(counter1+counter));
});
$('.blueanswer').one('click', function(){
localStorage.setItem('bans', ++counter1);
$('.btotal').text(counter1 + " concordano");
$('.rtotal').text(counter + " non concordano");
$('.rgraphic').css("height", counter * 100/(counter1+counter));
$('.bgraphic').css("height", counter1 * 100/(counter1+counter));
});
正如你所看到的,在這個例子中,項目bans
和rans
存儲在localStorage
和我用計數器的項目。 我想寫完全相同的代碼,但存儲在我的file.txt
中的新bans
和rans
。我認爲這必須用AJAX來完成,但我不知道。
我覺得file.txt的有這樣必須寫道:
bans: 1; //casual number
rans: 5; //casual number
我希望你明白,非常感謝你。
它可以用AJAX完成,我認爲這意味着你需要讀一本書或找到一些教程 – RiggsFolly
我不能創建在YouTube上沒有任何東西,我不明白如何在我的代碼中插入我需要的代碼。 –
YouTube並不是唯一的資源,但是如果你嘗試它,GOOGLE幾乎可以找到任何東西 – RiggsFolly