js或jquery报错Cannot read property 'length' of undefined(Cannot read property 'rows' of null)
程序员文章站
2022-06-30 22:18:17
js报错Cannot read property 'rows' of null是什么问题引起的?有时错误为Cannot read property 'length' of undefined...
js报错Cannot read property 'rows' of null是什么问题引起的?有时错误为Cannot read property 'length' of undefined,引起这两种报错的问题都是js或者jquery没有获取到对象,对象为null或者undefined。
如此代码:
document.addEventListener("DOMContentLoaded", function(e){ var table = document.getElementById("table"), sumVal = 0; for(var i =1; i < table.rows.length; i++) { sumVal = sumVal + table.rows[i].cells[6].innerHTML; } console.log(sumVal); });
自己写的语句就是没有问题,写法也是对的,对象id或者class也是存在的,就是获取不到对象,报错误呢?实际是页面加载的顺序问题,你js脚本获取对象之前,要先加载存在对象,所以等页面加载完之后,再执行你的js操作即可获取对象,也不会报错。
比如使用:
$(function(){});
以上就是关于null或undefined报错的总结,希望对你有所帮助!
推荐阅读
-
js报错 Cannot read property 'getAttribute' of null
-
js报错 Cannot read property 'getAttribute' of null
-
js或jquery报错Cannot read property 'length' of undefined(Cannot read property 'rows' of null)
-
echarts js报错 Cannot read property 'getAttribute' of null
-
关于JS报错:Cannot read property 'getAttribute' of null问题
-
js报错:Cannot read property 'getAttribute' of null问题
-
CocosCreator场景动态加载预置物体,二次加载场景报错TypeError: Cannot read property 'length' of null
-
js或jquery报错Cannot read property 'length' of undefined(Cannot read property 'rows' of null)
-
js 报错 Uncaught TypeError: Cannot read property 'trim' of undefined
-
echarts js报错 Cannot read property 'getAttribute' of null