我构建了一个简单的程序来学习 Javascript 中的 OOP。它仅输出预定义的字符串语句。这里的问题是,程序将文本节点直接绑定(bind)到“输出 div”,而忽略前面的命令将它们附加到相应的“p”元素。
Student.prototype.sayHello = function()
{
var responseString = document.createTextNode("Hello, I've been waiting here for you. Student.");
return document.createElement("p").appendChild(responseString);
}
Student.prototype.sayGoodBye = function()
{
var responseString = document.createTextNode("tchau");
return document.createElement("p").appendChild(responseString);
}
var student = new Student();
document.getElementById("output").appendChild(student.sayHello());
document.getElementById("output").appendChild(student.walk());
document.getElementById("output").appendChild(student.sayGoodBye());
最佳答案
你的问题是appendChild(child)
返回 child
但您似乎希望返回添加的 p
元素。
尝试做
var p = document.createElement("p");
p.appendChild(responseString);
return p;
关于javascript - document.createElement() 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17903209/