-
Notifications
You must be signed in to change notification settings - Fork 0
/
123123.html
60 lines (58 loc) · 2.04 KB
/
123123.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>天气和时间</title>
<script>
function getWeather(city) {
var api_url = "http://api.xn--7gqa009h.top/api/tianqi?msg=" + city;
var xhr = new XMLHttpRequest();
xhr.open("GET", api_url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var result = JSON.parse(xhr.responseText);
document.getElementById("weather").innerText = "天气:" + result.weather + ",温度:" + result.temperature + "℃";
}
};
xhr.send();
}
window.onload = function() {
// 获取用户所在城市
var url = "http://ip-api.com/json/";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var result = JSON.parse(xhr.responseText);
var city = result.city;
getWeather(city);
}
};
xhr.send();
// 更新当前时间
setInterval(function() {
var now = new Date();
var hour = now.getHours();
if (hour < 10) {
hour = "0" + hour;
}
var minute = now.getMinutes();
if (minute < 10) {
minute = "0" + minute;
}
var second = now.getSeconds();
if (second < 10) {
second = "0" + second;
}
var timeString = hour + ":" + minute + ":" + second;
document.getElementById("time").innerText = "当前时间:" + timeString;
}, 1000);
};
</script>
</head>
<body>
<h1>天气和时间</h1>
<p id="weather"></p>
<p id="time"></p>
</body>
</html>