1
在讓我的AJAX與另一個問題的幫助下工作後,我問了一下,我想創建另一個包含我的函數的文件,以保持我的代碼清潔。我沒有在網上找到任何有用的東西,所以我想這可能是不可能的。這是我想提取代碼:從文件中提取AJAX
<script> <!-- overall co2 -->
var co2;
var url="/solarpv/api/co2/list"
var jsonObject;
$(document).ready(function(){
$.getJSON(url,function(result){
jsonObject = result;
co2 = result[0].Cumulative_CO2;
$('#ajaxRequest').html("Our solar panels have saved " + co2 + " pounds of CO2 since they were installed.");
});
});
<!-- today co2 -->
var co2today;
var url2="/solarpv/api/co2/today"
$(document).ready(function(){
$.getJSON(url2,function(result){
co2today = result[0].CO2;
$('#today').html("Our Solar Panels have saved " + co2today + " pounds of c02 so far today.");
});
});
<!-- yesterday's CO2 -->
var url3 = "/solarpv/api/co2/list?start=2013-04-28%2001:00:00&end=2013-04-29%2001:00:00";
var yesterdayCO2;
$(document).ready(function(){
$.getJSON(url3,function(result){
yesterdayCO2 = result[0].Cumulative_CO2;
$('#yesterday').html("Yesterday alone, our solar panels saved the same amount of CO2 it would take " + yesterdayCO2/1.98 + " people to create!");
});
});
<!-- last years's CO2 -->
var url4 = "/solarpv/api/co2/list?start=2012-04-28%2001:00:00&end=2013-04-29%2001:00:00";
var trees;
$(document).ready(function(){
$.getJSON(url4,function(result){
trees = result[0].Cumulative_CO2;
$('#yesterday').html("Last year our solar panels saved the equivalent of " + trees/48.061 + " trees worth of C02");
});
});
</script>
使用這個貌似會在文件中留下的HTML的一個例子:
<li id="yesterday">
<script>
document.write("Yesterday alone, our solar panels saved the same amount of CO2 it would take " + yesterdayCO2 + " people to create!");
</script>
</li>
哇,快速響應!非常好,謝謝! – Mike 2013-05-01 19:14:22
簡單的問題,並提出。 – 2013-05-01 19:15:10
謝謝!通常我會受到SO社區的重創。 3分鐘,直到我可以接受這個答案。 – Mike 2013-05-01 19:15:59