javascript 无法在“节点”上执行“appendChild”:参数 1 的类型不是“节点”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28217755/
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
Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'
提问by Jarrod Farrell
Having a bit of an issue that might be staring me right at the face.
有一些问题可能正盯着我的脸。
var listitem = document.createElement("li").id = 'list' + x[i]
var listitem = listitem.innerHTML = '<button id="delete' + x[i] + '" class="button action" onclick="deleteuser(\'' + x[i] + '\')">Delete</button>' + x[i]
document.getElementById('userslist').appendChild(listitem)
The bit of code is in a for loop on Javascript and from the error I can understand that the for loop is working as intended, but it's failing to append to the mentioned list with the error stated above.
这段代码在 Javascript 上的 for 循环中,从错误中我可以理解 for 循环按预期工作,但它未能通过上述错误附加到提到的列表中。
回答by epascarello
This is wrong
这是错误的
var listitem = document.createElement("li").id = 'list' + x[i]
It ends up storing the string in the variable listitem
and not the object.
它最终将字符串存储在变量中listitem
而不是对象中。
Your code should be
你的代码应该是
var listitem = document.createElement("li");
listitem .id = 'list' + x[i];