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
Release: 2016-05-16 18:56:47
Original
949 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template