你好堆垛机!
我正在尝试在网页上将美元兑换成南非兰特。
我有一个从这里获得的 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;
预先感谢您的协助!