2013-07-27 3 views
0

자바 스크립트에서 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; 
을하고