簡體   English   中英

如何用變量替換javascript yahoo weather api調用屬性

[英]how to substitute a javascript yahoo weather api call property with a variable

這是一個可以正常工作並讀取yahoo weather api的腳本

<script>
  var callbackFunction = function(data) {
    var windy = data.query.results.channel.wind;
    alert(windy.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>

但是我需要將變量替換為var windy = data.query.results.channel.wind;

var select = "results";
var windy = data.query.select.channel.wind;

我沒有工作。 我定義了函數內部和外部的var,沒有區別。 我使用+ select +,它也導致了錯誤。 我的目標是要有一個帶有下拉菜單的頁面,供用戶選擇天氣項目並將選擇內容提供給上面的查詢,因此需要變量。 請幫忙,謝謝。

嘗試這個:

var select = "results";
var windy = data.query[select].channel.wind;

使用[]表示法時,您可以根據需要傳遞動態屬性名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM