首页 > 解决方案 > 即使有元素也无法访问 HTMLCollection

问题描述

当条件满足时,我会尝试更改一些元素。我需要xTick[0],但即使有 HTMLColletion 它总是未定义

var xTick = document.getElementsByClassName('some class name')

控制台日志

console.log(xTick)
output:
    HTMLCollection []
    > 0: text
    > 1: text
    > 2: text
    ...
    > 8: text
    > 9: text
      length: 10
    > __proto__: HTMLCollection


console.log(xTick.length)
output: 0


console.log(xTick[0])
output: undefined

console.log(xTick)有正确的输出console.log(xTick[0])console.log(xTick.length)没有

标签: javascripthtml

解决方案


推荐阅读