children". It would be better to use "childNodes" instead of the "children". "childNodes" is supported by all major browsers including IE, while "children" (invented by IE) is not well supported by major browsers other than IE. The former conforms to the standard while the later is not.

">
立诚勿怠,格物致知
It's all about connecting the dots

Uncaught TypeError: Cannot read property 'children' of null

1. JavaScript Code Snippet:


function slideTop(idname, topname) {
    var headname = document.getElementById(idname);
    var divname = document.getElementById(topname);
    var alen = divname.children.length;
    ...
}

2. Error:

Uncaught TypeError: Cannot read property ‘children’ of null

3. Analysis:

The error returned by the browser says that the property ‘children‘ of null can not be read, that is to say, the variable divname equals to null, because ‘children‘ is the property of divname. Therefore, it can be determined that the “getElementById(topname)” equals to null, i.e. the element with the id “topname” can not be got. So there are mainly two reasons accounting for this error returned by browser:

  1. There may be something wrong with the id “topname“, e.g. the correct id may be “topName“.
  2. Maybe the element with the id “topname” is existed but can not be got by the broswer due to that this function is executed before the element is interpreted. This case is usually seen when the JavaScipt code snippet is put inside the HTML opening <head> tag and the closing </head> tag while the function is not related to the window.onload event. To solve this problem, you can connect the function with the window.onload event, or move this JavaScript code snippet to where just before the HTML closing </body> tag.

P.S: You should not use “children“. It would be better to use “childNodes” instead of the “children“. “childNodes” is supported by all major browsers including IE, while “children” (invented by IE) is not well supported by major browsers other than IE. The former conforms to the standard while the later is not.

赞(1) 打赏
文章名称:《Uncaught TypeError: Cannot read property 'children' of null》
文章链接:https://www.orzzone.com/uncaught-typeerror-cannot-read-property-children-of-null.html
商业联系:yakima.public@gmail.com

本站大部分文章为原创或编译而来,对于本站版权文章,未经许可不得用于商业目的,非商业性转载请以链接形式标注原文出处。
本站内容仅供个人学习交流,不做为任何投资、建议的参考依据,因此产生的问题需自行承担。

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力提供更多优质内容!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册