这两天在学习js,发现一个问题,我在用appendChild动态插入一个input type=text标签时,发现不能设置这个文本框所响应的事件,代码是这样写的:
var oTextInDiv = document.createElement("input");
oTextInDiv.type = "text";
oTextInDiv.id = "textInDivId";
oTextInDiv.width = width - 5;
oTextInDiv.height = height - 5;
oTextInDiv.onblur = "removeChildDiv(this)";
childDiv.appendChild(oTextInDiv);
当所创建的文本框失去焦点时,一直都不执行"removeChildDiv(this)"方法。
然后我选择用insertAdjacentHTML方法插入一段HTML代码:
var inputString = "<INPUT id=textInDivId onblur='removeChildDiv(this)' width='100px' height='30px'/>";
childDiv.insertAdjacentHTML("afterBegin", inputString);
这样"removeChildDiv(this)"方法是执行了,但是新的问题又出现了,这样插入的方式我设置的属性又不起作用了,无语...
没有办法,只能把两个方法配合起来用:
var inputString = "<INPUT id=textInDivId onblur='removeChildDiv(this)'>";
childDiv.insertAdjacentHTML("afterBegin", inputString);
var textBox = document.getElementById("textInDivId");
textBox.height = height - 6;
textBox.width = width - 6;
不知道是我什么地方操作有问题,还是DHTML本来就是这个特性,不知道还有没有什么更好的方法,希望各位大虾指教。







然后我选择用insertAdjacentHTML方法插入一段HTML代码:


没有办法,只能把两个方法配合起来用:





