Home >Web Front-end >JS Tutorial >Event implementation code in dynamically created table cells_javascript skills

Event implementation code in dynamically created table cells_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:56:47937browse

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn