Uncaught TypeError:无法读取null的属性'getElementsByTagName' [英] Uncaught TypeError: Cannot read property 'getElementsByTagName' of null

查看:2002
本文介绍了Uncaught TypeError:无法读取null的属性'getElementsByTagName'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决方案,但是 .getElementsByTagName() which used,产生了一个错误:

I'm trying this solution but .getElementsByTagName() which is used, produced an error:

var tags = document.body.getElementsByTagName("*");
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null

为什么会产生这个错误如何修复它?

Why is it producing this error and how can I fix it?

推荐答案

document.body won'

< script> < head> 元素添加到< body> 元素中(因为您试图获取所有元素它必须位于body元素的 end ),或者将其放入一个函数中,并从创建正文后触发的事件调用它(例如 window load event)。

Move the <script> from the <head> element to the <body> element (since you are trying to get all the elements in it, you'd want to be at the end of the body element) or put it in a function and call it from an event that fires after the body has been created (such as the window load event).

这篇关于Uncaught TypeError:无法读取null的属性'getElementsByTagName'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆