为了账号安全,请及时绑定邮箱和手机立即绑定

弹出错误:Cannot read property 'nodeName' of null" 为什么呢?

弹出错误:Cannot read property 'nodeName' of null" 为什么呢?

qq_ggJKING_LWJ_0 2016-10-13 09:54:57
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style> </style> <script> window.onload = function(){ var fn={ plus1:function(a,b){  //var plus=function(a,b){  不能这样,因为自己已在变量的定义里 return a+b; }, minus1:function(a,b){ return a-b; } } //alert(fn.plus1); document.write(fn.plus1(1,2)+'<br />'); document.write(fn.minus1(3,2)+'<br />'); var odiv=document.getElementById("div"); document.write(odiv.nodeName+'<br />'+odiv.nodeType+'<br />'+odiv.nodeValue); } </script> </head> <body> <div id="div">12</div> </body></html>弹出错误:Cannot read property 'nodeName' of null"  为什么呢?
查看完整描述

2 回答

?
Caballarii

TA贡献1123条经验 获得超629个赞

var odiv=document.getElementById("div");这句被你写在window.onload外面了,就不会在div加载之后执行,取不到所以会是null。

建议弄个编辑器把代码格式化一下,要不然很容易看不清大括号括到哪里,比如你这里minus1后面就多了一个大括号,导致后面的代码在window.onload外面了

查看完整回答
反对 回复 2016-10-13
  • qq_ggJKING_LWJ_0
    qq_ggJKING_LWJ_0
    var odiv=document.getElementById("div");这句被你写在window.onload外面了 ....看清楚是在里面的。我知道问题了。是那个 document.write 的问题 把div覆盖了,所以div 为null。。。后续要注意 document.write的问题。
?
小董ong123456

TA贡献3条经验 获得超0个赞

刷新一下页面

查看完整回答
反对 回复 2016-10-13
  • 2 回答
  • 0 关注
  • 5786 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信