代码如下
var tableElem = document.getElementById("MyTable");
var tbodyElem = tableElem.getElementsByTagName("tbody")[0];
var trElem = document.createElement("tr");
var td1 = document.createElement("td");
td1.onclick = "EditCell()";
td1.width = "45%";
td1.innerText = " ";
trElem.appendChild(td1);
tbodyElem.appendChild(trElem);
alert(tbodyElem.innerHTML);
这段代码动态创建一个tr和td,并在td中设置了一个onclick事件,但是事实却是当点击td时,并没有出发点击时间调用EditCell函数。后来吧td1.onclick = "EditCell()";改成td1.attachEvent("onclick",EditCell)才成功调用
时间: 2024-11-11 03:05:17