JS:遍历页面并将美元转换为兰特

JS: Iterate over page & convert US Dollars to Rands

我有一个client website。这是测试登录详细信息 Login: TestOne, Password: password.

我正在尝试将页面上的所有值都转换为美元。我已将一些标称值订阅(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');

我试图使用这种回调函数格式,希望 classback 函数可以遍历页面:

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"}}}});

如果您能提供任何帮助,我将不胜感激!

您的客户网站有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');