我正在动态创建一个 html 表。这里,对于每一行,通过递归调用创建两列。简而言之,当单击第二个框时,会创建一个新行,依此类推。但我想用“Enter”键代替点击。我尝试过,代码可以通过单击创建新行,但不能通过按 Enter 键来创建新行。
function CreateRow(){
// 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();
// 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);
/** $(cell2).bind('click', function() {
CreateRow();
});
*/
$(cell2).keydown(function (e){
if(e.keyCode == 13){
CreateRow();
}
});
// Add some text to the new cells:
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}
请帮忙。
最佳答案
不要将 keydown 事件绑定(bind)到表格单元格,而是将其绑定(bind)到文档。
$(document).keydown(function (e) {
关于javascript - 按 Enter 键创建表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28117639/