gpt4 book ai didi

javascript - JS : Iterate over page & convert US Dollars to Rands

转载 作者:行者123 更新时间:2023-12-03 03:54:29 30 4
gpt4 key购买 nike

我有一个client website 。以下是测试登录详细信息登录: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"}}}});

非常感谢您提供的任何帮助!

最佳答案

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

关于javascript - JS : Iterate over page & convert US Dollars to Rands,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45015331/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com