weixin_33739523 2015-12-14 18:22 采纳率: 0%
浏览 190

For循环onclick按钮

<button type="button" onclick="loadDoc()">Scimba text</button>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    for (var i = 1; i<5; i++){
      if (xhttp.readyState == 4 && xhttp.status == 200) {
        document.getElementById(i).innerHTML = xhttp.responseText;
      }
    }
  };
  xhttp.open("GET", "info1.txt", true);
  xhttp.send();
}
</script>

Hello everybody ! Need some help ... How can I make the for loop to iterate one time when I click the button and when the button is clicked for the second time the loop will iterate also the second time and so on ...?

  • 写回答

4条回答 默认 最新

  • weixin_33738578 2015-12-14 18:27
    关注

    You should use a counter.

    Declare a global variable (with var) and increase it every time you loop.

    评论

报告相同问题?