0

我有一个客户网站。以下是测试登录详细信息登录:TestOne,密码:密码。

我正在尝试将页面上的所有值转换为美元。我已将一些对标称值的订阅(Neo Natal:0-1 月和 Neo Natal:1-2 月)更改为标称值以测试我的功能:

function callbackTester(callback, cstmClass) {
  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%3DUSDZAR%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
  document.body.appendChild(script);
}

function tryMe(cstmClass) {
  var name = data.query.results.row.name;
  var rate = Math.round(parseFloat(data.query.results.row.rate, 10));

  console.log('Rand - Dollar exchange rate: ' + rate);
  var priceConvert = document.getElementsByClassName(cstmClass);

  for (var i=0; i<priceConvert.length; i++) {
    var currentPrice = priceConvert[i].innerHTML;
    priceConvert[i].innerHTML = 'R' + (currentPrice * rate) + '.00';
  }
}

callbackTester(tryMe, 'price-convert');

我试图使用这种回调函数格式,希望类回函数能够遍历页面:

function callbackTester (callback, param1, param2) {
    callback (param1, param2);
}

function tryMe (param1, param2) {
    alert (param1 + " and " + param2);
}

callbackTester (tryMe, "hello", "goodbye");

但我收到以下错误: ReferenceError: parseExchangeRate is not defined

...关于功能: parseExchangeRate({"query":{"count":1,"created":"2017-07-10T14:30:37Z","lang":"en-GB","results":{"row":{"rate":"13.3762","name":"USD/ZAR"}}}});

我将不胜感激您能提供的任何帮助!

4

1 回答 1

1

您的客户端网站有 jQuery,那么您可以像使用它一样使用它

function callbackTester(callback, cstmClass) {
    jQuery.ajax({
    jsonp: "callback",
    dataType: "jsonp",
    url: "https://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%3DUSDZAR%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json",
    success: function(data){
        console.log(data);
        callback(data, cstmClass);
    }
  });
}

function tryMe(data, cstmClass) {
  var name = data.query.results.row.name;
  var rate = Math.round(parseFloat(data.query.results.row.rate, 10));

  console.log('Rand - Dollar exchange rate: ' + rate);
  var priceConvert = document.getElementsByClassName(cstmClass);

  for (var i=0; i<priceConvert.length; i++) {
    var currentPrice = priceConvert[i].innerHTML;
    priceConvert[i].innerHTML = 'R' + (currentPrice * rate) + '.00';
  }
}

callbackTester(tryMe, 'price-convert');
于 2017-07-10T15:42:33.637 回答