This article mainly introduces in detail the small application of native js to query weather, which has a certain reference value, interested friends can refer to this article for details about the small application of native js to query weather, which has a certain reference value, interested friends can refer to the examples in this article to share with you the js weather query application for your reference. The specific content is as follows:
Function: When a webpage is opened, the weather conditions of the city in which the user is located are displayed. In the input box, enter the city to query the weather conditions of other cities.
Implementation process: first call the Baidu map API to obtain the user's city, and then call the weather API to aggregate data to put the data on the page. Because ajax does not support cross-origin, jsonp is used to call data.
The implementation principle is relatively simple, HTML and css are relatively long, I just paste the js Code, want to see the complete code
// Call the jsonp function to request jsonp (' https://api.map.baidu.com/api?v=2.0&ak=Dv1NMU23dh1sGS9n2tUouDEYY96Dfzh3&s=1&callback=getCity '); Window. onload = function () {// request the weather vehicle data btn. onclick = function () {jsonp (createUrl () ;}}; function getCity () {function city (result) {jsonp (createUrl (result. name);} var cityName = new BMap. localCity (); cityName. get (city);} // Data Request function jsonp (url) {var script = document. createElement ('script'); script. src = url; document. body. insertBefore (script, document. body. firstChild); document. body. removeChild (script);} // callback function for successful data request, used to put the obtained data into the corresponding position of the page function getWeather (response) {var oSpan = document. getElementsByClassName ('info'); var data = response. result. data; oSpan [0]. innerHTML = data. realtime. city_name; oSpan [1]. innerHTML = data. realtime. date; oSpan [2]. innerHTML = 'Week' + data. weather [0]. week; oSpan [3]. innerHTML = data.realtime.weather.info; oSpan [4]. innerHTML = data. realtime. weather. temperature + '℃ '; oSpan [5]. innerHTML = data. realtime. wind. direct; oSpan [6]. innerHTML = data. realtime. weather. humidity + '%'; oSpan [7]. innerHTML = data. realtime. time; oSpan [8]. innerHTML = data.life.info. ziwaixian [0]; oSpan [9]. innerHTML = data.life.info. xiche [0]; oSpan [10]. innerHTML = data.life.info. kongtiao [0]; oSpan [11]. innerHTML = data.life.info. chuanyi [0]; var aDiv = document. getElementsByClassName ('future _ box'); for (var I = 0; IThere are still many shortcomings in a simple demo. You are welcome to propose suggestions for improvement.
I will update some of the problems and solutions I encountered here tomorrow. Please pay attention to them.
The above is all the content of this article. I hope it will help you learn and support PHP.
For more articles about simple implementation code of native js-level three linkage, please follow the PHP Chinese network!