我使用 jquery 和 json 数组在网站上显示当前的月相和日出/落下时间。这对于日出/日落效果很好,但对于月相我遇到了问题。
当“closestphase”与请求数据的同一天时,其他 2 个字段将被删除 - “currentphase”和“fracillum”。这意味着我无法每月显示 4 次相位或当前照明...
例如,下面的链接显示了 fracillum 和当前阶段: http://api.usno.navy.mil/rstt/oneday?date=today&coords=54.97N,1.61W&tz=0
但此数据中省略了: http://api.usno.navy.mil/rstt/oneday?date=01/28/2017&coords=54.97N,1.61W&tz=0
我编写了下面的代码来说明如果当前相位和分频未定义并且最近的相位是“新月”或“满月”,则显示分频为“0% 或“100%”分别。
请问有人可以指出我做错了什么吗?
$(document).ready(function(){
$.get("http://api.usno.navy.mil/rstt/oneday?date=01/28/2017&coords=54.97N,1.61W&tz=0", function(data){
checkPhase(data);
});
});
$(document).ready(function(){
$("#suntimes").click(function(){
$("#sun").toggle("slide");
});
});
$(document).ready(function(){
$("#moontimes").click(function(){
$("#moon").toggle("slide");
});
});
$(document).ready(function(){
$("#Mobilesuntimes").click(function(){
$("#Mobilesun").slideToggle();
});
});
$(document).ready(function(){
$("#Mobilemoontimes").click(function(){
$("#Mobilemoon").slideToggle();
});
});
function checkPhase(data){
if(data.curphase == "undefined" && data.closestphase.phase == "New Moon"){data.fracillum == "0%";}
else if(data.curphase == "undefined" && data.closestphase.phase == "Full Moon")
{data.fracillum == "100%";}
$("#sun").append("<p><b>Newcastle, UK</b><br />Sunrise: " + data.sundata[1].time + " AM<br />Sunset: " + data.sundata[3].time + " PM</p>" );
$("#moon").append("<p><b>Newcastle, UK</b><br />Percentage Illuminated: " + data.fracillum + "<br />Moon Phase: " + data.curphase + "</p>" );
$("#Mobilesun").append("<p><b>Newcastle, UK</b><br />Sunrise: " + data.sundata[1].time + " AM<br />Sunset: " + data.sundata[3].time + " PM</p>" );
$("#Mobilemoon").append("<p><b>Newcastle, UK</b><br />Percentage Illuminated: " + data.fracillum + "<br />Moon Phase: " + data.curphase + "</p>"
);
}
最佳答案
使用typeof data.curphase ==“undefined”
而不是data.curphase ==“undefined”
var a = 1;
if ( typeof a == "undefined" )
alert (" a is undefined" )
if ( typeof b == "undefined" )
alert (" b is Undefined" )
关于javascript - 使用 Jquery 显示来自 API 的 JSON 数据 - 显示未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42326845/