2013-07-27 151 views
0

我已經構建了一個簡單的程序來學習Javascript中的OOP。它只輸出預定義的字符串語句。這裏的問題是程序將文本節點直接綁定到「輸出div」,而忽略了前面的命令將它們附加到相應的「p」元素。document.createElement()被忽略

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()); 

回答

3

您的問題是appendChild(child)回報child但似乎你希望退回補充p元素。

嘗試做

var p = document.createElement("p"); 
p.appendChild(responseString); 
return p;