javascript for in loop returning undefinded

后端 未结 1 1234
予麋鹿
予麋鹿 2021-01-28 12:40

Looking for help with a for in loop I am using in which returns undefined at the the end.

I have 26 buttons, one button for each letter in the alphabet. I need to scan

1条回答
  •  自闭症患者
    2021-01-28 12:57

    You should not use a for-in loop over a collection. If you do you need to test hasOwnProperty

    Instead use a simple loop

    var item = document.getElementsByClassName('button');
    // or better: var item = document.querySelectorAll('.button');
    for (var i=0;i

    var item = document.getElementsByClassName('button');
    // or better: var item = document.querySelectorAll('.button');
    for (var i = 0; i < item.length; i++) {
      item[i].onclick = function() {
        console.log(this.value);
      };
    }

    Game



    0 讨论(0)
提交回复
热议问题