天气应用程序Javascript无法工作
Weather app Javascript isn't working
控制台什么都没有。它应该将html的包含内容更改为从openweatherapp获取的此邮政编码的信息(我检查此zip,它存在并且函数updateByZip创建良好的链接)。第一个代码是JS,第二个是HTML。JS
var temp;
var loc;
var humidity;
var icon;
function bg() {
var backs = ["http://wallpapercave.com/wp/JthAGYd.jpg", "http://www.desktopwallpaperhd.net/wallpapers/20/b/welshdragon-landscapes-cometh-background-204971.jpg", "http://s1.picswalls.com/wallpapers/2014/08/08/scottish-landscape-desktop-backgrounds_015751372_152.jpg", "http://img.wallpaperfolder.com/f/4313075B95B2/amazing-winter-backgrounds-6770538-landscape.jpg"];
var ran = Math.floor(Math.random() * (backs.length));
$('body').css("background-image", "url('" + backs[ran] + "')")
//document.body.style.background = "url/'('" + backs[0] + "') no-repeat";
}
function tempCF() {
var x = document.getElementById("temp").innerHTML;
var y = document.getElementById("CF").innerHTML;
x = parseInt(x);
if (y == "C") {
x = Math.floor((9 / 5 * x + 32));
document.getElementById("temp").innerHTML = x;
document.getElementById("CF").innerHTML = "F"
} else if (y == "F") {
x = Math.floor((x - 32) * 5 / 9);
document.getElementById("temp").innerHTML = x;
document.getElementById("CF").innerHTML = "C";
};
}
function updateByZip(zip) {
var APPID = "55e568aa04114cdf3dc4b90c9ae0a60c";
var url = "api.openweathermap.org/data/2.5/weather?zip=" + zip + "&APPID=" + APPID;
sendRequest(url);
}
function sendRequest(url) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
var weather = {};
weather.temp = data.main.temp;
weather.humidity = data.main.pressure;
weather.loc = data.name;
//weather.icon = data.weather[0].id;
update(weather);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function update(weather) {
document.getElementById("temp").innerHTML = weather.temp;
document.getElementById("humidity").innerHTML = weather.humidity;
document.getElementById("loc").innerHTML = weather.loc;
//document.getElementById("icon").innerHTML = weather.icon;
};
window.onload = function() {
var temp = document.getElementById("temp").innerHTML;
var loc = document.getElementById("loc").innerHTML;
var humidity = document.getElementById("humidity").innerHTML;
//var icon = document.getElementById("icon").innerHTML;
updateByZip(94040);
//weather.icon = "https://cdn1.iconfinder.com/data/icons/hawcons/32/700175-icon-1-cloud-128.png";
};
HTML <head>
<link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
</head>
<body onclick="bg()">
<div class="container-fluid">
<div class="wrapper">
<div class="row">
<div class="title"><span id="loc"> Your location</span></div>
</div>
<div class="row">
<div class="col-ms-4">
<div class="fircol"><span id="temp">0</span>°<span onclick="tempCF()" id="CF">C</span></div>
</div>
<div class="col-ms-4">
<div class="seccol"><span id="humidity">Rain</span></div>
</div>
<div class="col-ms-4">
<div class="thicol"><span class="icon"><img src= https://cdn1.iconfinder.com/data/icons/hawcons/32/700175-icon-1-cloud-128.png></img></span></div>
</div>
</div>
</div>
</div>
</body>
您的API url不正确。你应该写:
var url = "http://api.openweathermap.org/data/2.5/weather?zip=" + zip + "&APPID=" + APPID;
相关文章:
- 用户名输入如果其他块不能正常工作/Javascript-jQuery-AJAX
- 变量只能在函数中局部工作,不能全局工作-Javascript
- onClick按钮不工作javascript/php/jquery mobile
- 点击骰子赢得't工作(javascript)
- for循环,if else不工作Javascript
- 获取下一个不工作Javascript DOM的同级父节点的子节点
- 可以'没有一个函数可以工作——Javascript
- OnFocus 不是工作 JavaScript HTML;不显示对焦时计算和按钮单击时
- 从 iframe 访问主站点中的工作 JavaScript
- 对于在循环中无法按预期工作 JavaScript
- 在阅读表单输入后写回HTML;t工作(Javascript)
- 复选框单击有效,取消选中复选框不工作Javascript
- 代码在Codepen中工作,而不是在浏览器中工作:JavaScript在鼠标滚轮上缩放SVG
- 当用户在这个工作javascript注释框中提交注释时,您将如何添加用户图像和名称
- 向Wordpress添加工作Javascript谷歌地图代码
- 确认/重定向功能不工作-javascript
- 当/patt/版本工作时,为什么RegExp版本不工作?(Javascript)
- Firefox加载项按钮代码不工作-Javascript
- 计算器功能不工作- Javascript
- 将工作JavaScript对象序列化为仅保留属性的JSON