Javascript转换货币

时间:2017-07-07 09:58:17

标签: javascript currency-exchange-rates

你好堆叠人员!

我想在网页上将美元转换为南非兰特

我有一个从 here 获得的Javascript功能。

该功能正常,但我似乎无法为我返回一个值:/

function getRate(from, to) {
  var script = document.createElement('script');
  script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D"+from+to+"%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
  document.body.appendChild(script);
}

function parseExchangeRate(data) {
  var name = data.query.results.row.name;
  var rate = parseFloat(data.query.results.row.rate, 10);
  console.log('rate: ' + rate);
  return rate;
}

var dollarRand = getRate("USD", "ZAR");

var priceConvert = document.getElementsByClassName('price-convert');
var currentPrice = priceConvert[0].innerHTML;
currentPrice = currentPrice * dollarRand;

提前感谢您的协助!

0 个答案:

没有答案