当放置在html doc的head或body部分时,这些脚本会立即写入文档。如何从文档中的按钮调用其功能。我已经尝试了调用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>
答案 0 :(得分:2)
看看这段代码。我得到风寒,把它存放在一个变量中。然后,我使用按钮的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" />
&#13;
注意:当页面加载时,这会导致风寒。如果您需要在每次单击按钮时获得新的风寒(不进行页面重新加载),则需要修改上述代码。