这是我的js文件:
window.onload=function(){
var anima = document.getElementById("crypto");
var ret = document.getElementById("btn");
ret.addEventListener("click",function(){
var ourRequest = new XMLHttpRequest() ;
ourRequest.open('GET','https://api.cryptonator.com/api/full/btc-usd');
ourRequest.onload = function(){
var fdata = JSON.parse(ourRequest.responseText);
//renderHTML(fdata);
console.log( fdata);
};
ourRequest.send()
});
当我点击按钮时,BTC价格必须在hmtl页面的div区域中可见。
我怎样才能做到这一点? 这是网址https://api.cryptonator.com/api/full/btc-usd
div的id是加密的。
答案 0 :(得分:0)
window.onload=function(){
var anima = document.getElementById("crypto");
var ret = document.getElementById("btn");
ret.addEventListener("click",function(){
var ourRequest = new XMLHttpRequest() ;
ourRequest.open('GET','https://api.cryptonator.com/api/full/btc-usd');
ourRequest.onload = function(){
var fdata = JSON.parse(ourRequest.responseText);
// anima – your div with #crypto
// fdata.ticker.price – your path in api to BTC price;
anima.textContent = fdata.ticker.price;
};
ourRequest.send()
});
如果您将来要插入html标签,也可以尝试使用innerHTML。
https://developer.mozilla.org/ru/docs/Web/API/Node/textContent
https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML