Jquery 从json url文件读取并计算一个值
我想展示一下我的0.128欧元硬币是多少欧元。 如何计算欧元价格乘以0.128Jquery 从json url文件读取并计算一个值,jquery,json,Jquery,Json,我想展示一下我的0.128欧元硬币是多少欧元。 如何计算欧元价格乘以0.128 <!DOCTYPE html> <html lang="en"> <head> <title>JavaScript - read JSON from URL</title> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> </head
<!DOCTYPE html> <html lang="en"> <head> <title>JavaScript - read JSON from URL</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> </head>
<body>
<div class="mypanel"></div>
<script>
$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR', function(data) {
var text = `0.128 * parseFloat(${data.data.quotes.EUR.price})`
$(".mypanel").html(text);
});
</script>
</body> </html>
JavaScript-从URL读取JSON
$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR,函数(数据){
var text=`0.128*parseFloat(${data.data.quotes.EUR.price})`
$(“.mypanel”).html(文本);
});
在函数中,您不应以类似JQuery的方式使用数据。内部是纯JavaScript,如:
$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR,函数(数据){
var res=0.128*parseFloat(data.data.quotes.EUR.price)
$(“.mypanel”).html(res);
});代码>
JavaScript-从URL读取JSON
您不必解析结果:结果已经是浮点类型
如果你想的话,可以把它围起来
以下是工作代码:
JavaScript-从URL读取JSON
$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR,函数(数据){
var text=0.128*data.data.quotes.EUR.price
$(“.mypanel”).html(文本);
});
和一个可玩的JSFIDLE:
var text=0.128*data.data.quotes.EUR.price代码>?为什么使用模板文本…反勾选?
<script>
$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR', function(data) {
var text = 0.128 * data.data.quotes.EUR.price
$(".mypanel").html(text);
});
</script>
</body> </html>