JavaScript未捕获TypeError:无法读取null的属性'firstChild' [英] javascript Uncaught TypeError: Cannot read property 'firstChild' of null

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

问题描述




我在Google Chrome浏览器中看到以下错误信息, p> Uncaught TypeError:无法读取null的属性'firstChild'

该错误适用于以下js elem.firstChild。 nodeValue = dispmessage;

 函数msg(fld,
msgtype,
message) {
if(emptyString.test(message))
dispmessage = String.fromCharCode(nbsp);
else
dispmessage = message;

var elem = document.getElementById(fld);
elem.firstChild.nodeValue = dispmessage;

elem.className = msgtype; //设置CSS类来调整消息
}的外观;

想知道有人遇到过类似的问题吗?任何想法如何解决?



干杯

解决方案

错误意味着 elem 对象是 null 。检查传递的fld值,看看具有该id的对象是否实际存在于您的代码中。


I am getting the following error in Google Chrome on a very old Form I inherited (seems to work OK in IE, no errors)

Uncaught TypeError: Cannot read property 'firstChild' of null

The error applies to the following js elem.firstChild.nodeValue = dispmessage;:

function msg(fld,     
         msgtype, 
         message) {
  if (emptyString.test(message))
    dispmessage = String.fromCharCode(nbsp);
  else
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;

  elem.className = msgtype;   // set the CSS class to adjust appearance of message
};

Wondering if someone has come across similar issue before? Any ideas on how I can resolve?

Cheers

解决方案

This error means the elem object is null. Check the fld value passed and see whether an object with that id actually exists in your code or not.

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

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