프로토 타입을 사용하여 H2 제목을 만들어서 필요에 따라 개별적으로 설정할 수 있습니다.프로토 타입 javascript를 사용하여 제목 스타일 설정
H2
에 텍스트를 추가하려면 this.appendChild(document.createTextNode(''));
을 사용하고 있습니다. 나는이 경우에 appendChild
이기 전에 this
키워드라고 말하기 전에 부모 노드를 사용해야하지만 부모로 인식되고 있는지 또는 실제로 부모인지는 확실하지 않습니다. 또한 생성자 자체의 매개 변수를 통해 텍스트를 추가하는 방법에 관해서도 확실하지 않습니다. 변수 '글꼴'을 사용했지만 CSS 스타일을 추가하지 않는 방법을 잘 모르겠습니다.
프로토 타입을 사용하는 방법을 배우고 있습니다. 내가 놓친 다른 명백한 오류가 있다면 알려주십시오.
<div id='body'>
<div id='inner'>div here</div>
</div>
<script>
Heading.prototype.font;
Heading.prototype.color;
Heading.prototype.fontSize;
Heading.prototype.headingTxt;
Heading.prototype.setHeading = function() {
var inner = document.getElementById('inner');
this.headingTxt = document.createElement('h2');
this.headingTxt.font = this.appendChild(document.createTextNode(''));
this.headingTxt.style.color = this.color;
this.headingTxt.style.fontSize = this.fontSize;
inner.appendChild(headingTxt);
}
function Heading(font, color, fontSize) {
this.font = font;
this.color = color;
this.fontSize = fontSize;
}
var title = new Heading('heading here', 'red', 20);
title.setHeading();
</script>
어떻게하면이 문제를 해결할 수 있습니까? 내가 옷을 벗었
상위 노드는 무엇입니까? 'this '는 DOM 엘리먼트가 아닌'Heading'의 인스턴스, 즉'title'을 참조합니다. –
분명히 'this'를 사용하는 것이 잘못되었습니다. 텍스트 노드가 자식이되도록 부모로 생성되는 'H2'요소를 참조하기를 원합니다. – moonshineOutlaw