Home >Web Front-end >JS Tutorial >Event implementation code in dynamically created table cells_javascript skills
The code is as follows
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);
This code dynamically creates a tr and td, and An onclick event is set in td, but the fact is that when td is clicked, the EditCell function is not called at the click time. Later, td1.onclick = "EditCell()"; was changed to td1.attachEvent("onclick",EditCell) before it was successfully called.