如何从 html 文档中调用 yahoo weather api javascript 函数

How to call yahoo weather api javascript function from within a html doc

本文关键字:weather yahoo api javascript 函数 调用 html 文档      更新时间:2023-09-26

当放置在html文档的头部或正文部分时,这些脚本会立即写入文档。如何从文档中的按钮调用它们的功能。我已经尝试了调用js函数的所有常用方法,但没有一种有效。请帮忙,非常感谢。

<script>
  var callbackFunction = function(data1) {
    var windy = data1.query.results.channel.wind;
    //alert(windy.chill);
document.write("Wind chill factor:" + windy.chill);
  };
</script>
<script src="https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='chicago, il')&format=json&callback=callbackFunction"></script>

看看这段代码。我得到风寒并将其存储在变量中。然后,我使用按钮的 onclick 属性将该值放入div 中。这有帮助吗?

<script>
var wind_chill;
  var callbackFunction = function(data) {
    var wind = data.query.results.channel.wind;
    wind_chill = wind.chill;
  };
  
function get_wind_chill(){
  document.getElementById('wind_chill').innerHTML = "Wind chill factor:" + wind_chill;
}
</script>
 
<script src="https://query.yahooapis.com/v1/public/yql?q=select wind from weather.forecast where woeid in (select woeid from geo.places(1) where text='chicago, il')&format=json&callback=callbackFunction"></script> 
<div id="wind_chill"></div>
<input type="button" onclick="get_wind_chill();" value="Get wind chill" />

注意:页面加载时,这会产生寒意。如果你需要在每次点击按钮时获得新的风寒(不做页面重新加载),那么你将需要修改上面的代码。