添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
逃跑的企鹅  ·  647 webpack中使用 ...·  12 小时前    · 
风流倜傥的麦片  ·  第三期 | ...·  8 月前    · 
严肃的麻辣香锅  ·  java - security ...·  2 年前    · 
犯傻的黄豆  ·  php导出PHPWord - 掘金·  2 年前    · 

当我在循环遍历dom元素的时候,报了想下面这种错误
Uncaught TypeError: Cannot read properties of undefined (reading 'classList')

我的代码是这样的

                allStopVfor(tdClass) {
                    setTimeout(() => {
                        const tdStopVfor = document.querySelectorAll(tdClass)
                        for (let i = 1; i <= tdStopVfor.length; i++) {    
                            tdStopVfor[i].classList.add('active')

然后在别的地方调用了它,最后控制台一直报那种错误

最后我在添加classList.add 之前添加了个判断

                allStopVfor(tdClass) {
                    setTimeout(() => {
                        const tdStopVfor = document.querySelectorAll(tdClass)
                        for (let i = 1; i <= tdStopVfor.length; i++) {
                            if (tdStopVfor[i]) {
                                tdStopVfor[i].classList.add('active')

最后问题解决了

最后的可能因素,应该是数据的问题吧

分类:
前端
标签: