javascript 未捕获的类型错误:无法读取空错误消息的属性“firstChild”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8371816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot read property 'firstChild' of null error message
提问by methuselah
I've been trying to pass through JS variables via the onChange event but keep receiving the
我一直在尝试通过 onChange 事件传递 JS 变量,但一直收到
Uncaught TypeError: Cannot read property 'firstChild' of null
未捕获的类型错误:无法读取 null 的属性“firstChild”
under Google Chrome. Here is my code below:
在谷歌浏览器下。这是我的代码如下:
artistID = album_select.substring(0, album_select.indexOf('/'));
albumID = album_select.substring(album_select.indexOf('/')+1, album_select.length);
var count = 0;
var tracklist = '';
for(track in albumlist[albumID][2]){
count++;
tracklist+="<option id='"+album+"'>"+track+"</option>";
}
hstr+="<form><select size='"+count+"' onChange='parent.loadSong(this.value, document.getElementById(\"album_select\").firstChild.nodeValue, document.getElementById(\"artistID\").firstChild.nodeValue)' style='font-size:2em;'>";
hstr+=tracklist;
hstr+="</form></select>";
回答by RightSaidFred
This part of the string:
字符串的这一部分:
document.getElementById(\"artistID\")
Is looking for an ID of "artistID"
.
正在寻找 的 ID "artistID"
。
I assume you meant to concatenate the variable.
我假设您打算连接变量。
"...document.getElementById('" + artistID + "')..."
Sounds like this one needs it too:
听起来这个人也需要它:
"...document.getElementById('" + album_select + "')..."