我正在使用this code向表中添加列和行。由Javascript生成的表格中的每個單元格的ID
現在我正在嘗試爲每個生成的單元添加一個id,如在Excel表中。
對於行分配0,1,2等等,對於列分配a,b,c等,因此第一個單元格的ID將爲0a
或a0
或類似內容。
UPDATE:here is my actual code
var table = document.getElementById("excelTable");
var input = table.getElementsByTagName("input");
var globalLen = input.length;
var arr = [], arrStr = [];
var numId = null, modStr = null, re = null, var1 = null, var2 = null, total = null, sign = null;
function createCell(cell, r, c) {
cell.innerHTML = "<input id='td" + r + c+ "' type='text' onfocus='doEdit(this.id);' onblur='doUpdate(this.value, this.id);' />";
}
function addRowId(tbl) {
return tbl.rows.length;
}
function addColId(tbl) {
for (i = 0; i < tbl.rows.length; i++) {
return tbl.rows[i].cells.length;
}
}
function appendRow(n) {
for (; n > 0; n--) {
var tbl = document.getElementById('excelTable'),
row = tbl.insertRow(tbl.rows.length),
i;
for (i = 0; i < tbl.rows[0].cells.length; i++) {
createCell(row.insertCell(i), addRowId(tbl), addColId(tbl));
}
}
}
function appendColumn(n) {
for (; n > 0; n--) {
var tbl = document.getElementById('excelTable'),
i;
for (i = 0; i < tbl.rows.length; i++) {
createCell(tbl.rows[i].insertCell(tbl.rows[i].cells.length), addRowId(tbl), addColId(tbl));
}
}
}
我有問題,每一個細胞分配唯一的ID。該ID必須包含行號(1,2,3等)中的一位數字和一位數字或列中的字母(1,2,3等或a,b,c等)
因此,最終它看起來像11,12等或1a,1b等。
該ID將被分配給該單元格中的input
。
我的目標是獲取單元格(行和列)的索引並將這些索引分配給ID。
您可以嘗試'yourcell.setAttribute(「id」,「uniqueIdentifier」);'您擁有的每個單元格和'uniqueidentifier'都可以從行和列中派生,如您所述。 – Si8
或'yourcell.id =「uniqueIdentifier」;'。請點擊「編輯」並將您的實際代碼添加到問題中,而不要依賴指向外部文章的鏈接。 – nnnnnn
好的推特狀態。等等...太久了? –