学习javascript遇到的一个问题

[code="javascript"]function EditInPlaceField(id, parent, value) {
this.id = id;
this.parent = parent;
this.value = value || "default value";
this.createElements();
};

EditInPlaceField.prototype = {
createElements : function() {
this.container = document.createElement("div");
this.parent.appendChild(this.container);
},
};

var my = new EditInPlaceField("my", parent, "hello world");
var parent = document.getElementById("div");[/code]

错误信息:[color=red]Uncaught TypeError: Object [object Window] has no method 'appendChild' [/color]

弄了大半天,试了很多种方法都不行。请问这是什么原因啊?

[code="html"]



function EditInPlaceField(id, parent, value) { this.id = id; this.parent = parent; this.value = value || "default value"; this.createElements(); }; EditInPlaceField.prototype = { createElements : function() { this.container = document.createElement("div"); this.container.innerHTML = this.value; this.parent.appendChild(this.container); }, }; var parent = document.getElementById("div"); var my = new EditInPlaceField("my", parent, "hello world");

var my = new EditInPlaceField("my", parent, "hello world");

var parent = document.getElementById("div");

这两句换换位置,第一眼看出的问题,换了位置不知道还行不行,另外是不是兄弟你手误。

[code="js"]
function EditInPlaceField(id, parent, value) {

this.id = id;

this.parent = parent;

this.value = value || "default value";

this.createElements();

};

EditInPlaceField.prototype = {

createElements : function() {

this.container = document.createElement("div");

[b]this.container.innerHTML = this.value;[/b] this.parent.appendChild(this.container);

},

};

var parent = document.getElementById("div");

var my = new EditInPlaceField("my", parent, "hello world");

[/code]
好用了,我又加了一句!~~为了测试。

你的ID=div的元素放在 这段javascript的下面了。

你可以把
var my = new EditInPlaceField("my", parent, "hello world");

var parent = document.getElementById("div");

放在onload方法里。