JavaScript未捕获TypeError:无法读取null的属性'firstChild' [英] javascript Uncaught TypeError: Cannot read property 'firstChild' of null
问题描述
我在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 isnull
. 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屋!