html 当前位置天气js,js动态获取当前城市天气参数

本文展示了如何使用JavaScript获取并展示天气预报和当前时间。通过调用天气API获取本地城市三天内的天气信息,并将数据转化为适配显示的格式。同时,还实现了获取当前时间的功能,包括小时和分钟,并提供了AM/PM的区分。代码中涉及到的城市定位、数据解析和DOM操作等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*获取天气参数*/

function refreshWeather() {

jQuery.support.cors = true;

var url = encodeURI("http://wthrcdn.etouch.cn/weather_mini?city=" + $scope.city);

$.get({

url: url,

dataType: "json",

async: false,

success: function (data) {

var list = data.data.forecast;

if (list.length<3) {

return;

}

var wList= [];for (var i= 0;i < 3; i++) {

var item= list[i];var high= item.high.split("")[1];

var low= item.low.split("")[1];

wList.push({

day: item.date.slice(-3),

type: item.type,

temperature: high + "/" + low

});

}

$scope.$apply(function () {

$scope.weatherList= wList;});

}

});

}

/*获取当前时间*/

function refreshDate() {

var nowDate = new Date();

var h = nowDate.getHours();

$scope.$apply(function () {

$scope.nowDate = zero(hours(h)) + "." + zero(nowDate.getMinutes());

$scope.ampm = h < 12 ? "AM" : "PM";

});

function hours(value) {

return value % 12;

}

function zero(str) {

str = "" + str;

if (str.length === 1) {

str = "0" + str;

}

return str;

}

}

/*获取本地城市地址--高德地图api*/

function refreshLocalCity(complete) {

AMap.plugin(‘AMap.CitySearch‘, function () {

new AMap.CitySearch().getLocalCity(function (status, result) {

if (status === ‘complete‘ && result.info === ‘OK‘) {

$scope.$apply(function () {

$scope.city = result.city;

});

complete && complete();

}

})

});

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值