我正在使用 W3Schools 中的这段代码片段在我的表格中创建一个新行:
// Find a <table> element with id="myTable":
var table = document.getElementById("myTable");
// Create an empty <tr> element and add it to the 1st position of the table:
var row = table.insertRow(0);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
// Add some text to the new cells:
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
创建之后,表格中的所有行都有自己的 onClick 函数,除了我刚创建的那一行,所以我的问题是,如何为该行指定一个 onClick 函数?
最佳答案
你也可以用
document.getElementById("myTable").innerHTML+="<tr onClick='myFunc();'></tr>"
或者按照您的方式使用
row.onClick="myFunc();"
或
row.setAttribute( "onClick", "javascript: myFunc();" )
关于javascript - 在 JavaScript 中使用 onClick 属性创建一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23676525/