![](/img/trans.png)
[英]how do I pass a parameter to a javaScript function which comes from the BasePage of my web page, on a click button event?
[英]How do I set an on click event for every cell that calls a function which takes id of cell as a parameter in JavaScript
在这里,我创建了一个表格并为每个单元格分配了一个 ID。 我想做的是这样的 cell1.onclik = MyFunction(this.id);
function createTable() {
var table = document.getElementById("tabela2");
for (let j = 0; j < k; j++) {
var row = table.insertRow(0);
for (let i = 0; i < l; i++) {
var cell1 = row.insertCell(0);
cell1.id = "celica" + i + "_" + j;
/*doesnt work: cell1.onclik = MyFunction(this.id); */
}
}
}
不要使用 onXXX 事件。 如果页面上有内容安全策略,它将无法正常工作。
你只需要听表
function createTable() { var table = document.getElementById("tabela2"); // document.body.appendChild(table); for (let j = 0; j < 4; j++) { var row = table.insertRow(0); for (let i = 0; i < 4; i++) { var cell1 = row.insertCell(0); cell1.textContent = "1"; cell1.id = "celica" + i + "_" + j; } } table.addEventListener("click", function(e) { console.log(e.target.id); console.log(e.target.tagName); }); } createTable();
<html><body><table id='tabela2'></table></body></html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.