多个GetElementByID不起作用,我如何循环Javascript?

问题描述 投票:-3回答:1

我有多个getElementById,它们根据列标题从Google表格中提取数据。当我只有一个getElementById它完美的工作,但一旦我添加一秒,它没有。我被告知我需要循环它们但不知道到底是怎么回事。如果你能帮助我循环它,我将非常感激。

function httpGetAsync(theUrl, callback) {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
      callback(xmlHttp.responseText);
  }
  xmlHttp.open("GET", theUrl, true); // true for asynchronous
  xmlHttp.send(null);
}

httpGetAsync('https://spreadsheet.glitch.me/? 
key = 1 JBbAHH1DFtO1r56lr94lUqd8H7qPcHncJskcPq0r96o ', function(response){
var json = JSON.parse(response);

document.getElementById("btm").innerHTML = json[0].btm;
});

document.getElementById("totalpoints").innerHTML = json[1].totalpoints;
});

document.getElementById("btm").innerHTML = json[1].btm;
});

document.getElementById("average").innerHTML = json[4].average;
});
javascript loops innerhtml getelementbyid
1个回答
3
投票

您正在使用额外的“})关闭回调函数;”在每个document.getElementById之后......

删除它们,你的代码应该工作。

function httpGetAsync(theUrl, callback)  {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.onreadystatechange = function() {
  if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
     callback(xmlHttp.responseText);
  }
  xmlHttp.open("GET", theUrl, true); // true for asynchronous
  xmlHttp.send(null);
}

httpGetAsync('https://spreadsheet.glitch.me/?key=1JBbAHH1DFtO1r56lr94lUqd8H7qPcHncJskcPq0r96o', function(response) {
var json = JSON.parse(response);

document.getElementById("btm").innerHTML = json[0].btm;
document.getElementById("totalpoints").innerHTML = json[1].totalpoints;
document.getElementById("btm").innerHTML = json[1].btm;
document.getElementById("average").innerHTML = json[4].average;
});
<div id="btm"></div>
<div id="totalpoints"></div>
<div id="average"></div>
© www.soinside.com 2019 - 2024. All rights reserved.