JavaScript Uncaught TypeError: Cannot read property 'value' of null
程序员文章站
2022-05-04 13:30:49
用 JavaScript 操作 DOM 时出现如下错误: 例如: 运行时出现如下错误: 问题出在 JS 运行的时候你的页面还没有加载完成,所以你的 JS 代码找不到你的页面元素,就会抛出这个问题。解决办法就是把 JavaScript 代码放在 body 的最后,例如: ......
用 javascript 操作 dom 时出现如下错误:
uncaught typeerror: cannot set property 'value' of null uncaught typeerror: cannot read property 'id' of undefined
例如:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script> var div = document.getelementbyid("测试1"); alert(div.id); alert(div.classname); alert(div.title); </script> <title>测试</title> </head> <body> <div id="测试1" class="测试2" title="测试3"> <span>0</span> <span>1</span> <span>2</span> <span>3</span> </div> </body> </html>
运行时出现如下错误:
问题出在 js 运行的时候你的页面还没有加载完成,所以你的 js 代码找不到你的页面元素,就会抛出这个问题。解决办法就是把 javascript 代码放在 body 的最后,例如:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>测试</title> </head> <body> <div id="测试1" class="测试2" title="测试3"> <span>0</span> <span>1</span> <span>2</span> <span>3</span> </div> <script> var div = document.getelementbyid("测试1"); alert(div.id); alert(div.classname); alert(div.title); </script> </body> </html>
推荐阅读
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object
-
Extjs4---Uncaught TypeError: Cannot read property ‘items’ of undefined
-
JavaScript Uncaught TypeError: Cannot read property 'value' of null
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#
-
el-tooltip组件中content使用Vue-i18n报错TypeError: Cannot read property ‘$t‘ of null
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object
-
ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法
-
Vue引入echarts报错Error in mounted hook: “TypeError: Cannot read property ‘getAttribute‘ of null“
-
vue3 使用axios 会报错“Uncaught TypeError: Cannot read property ‘get‘ of undefined”
-
Vue框架的学习中,使用substring方法时提示:Uncaught TypeError: Cannot read property ‘substring‘ of undefined的解决方法