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

Multiple GetElementByID doesn't work, how do I loop Javascript?

我有多个 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;
});

您正在使用额外的 "}); 关闭您的回调函数在每个 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>