我希望为单元格的第一行和第一列分配一个字母和一个数字(如 excel 表),行(cell.cellIndex == 0),没有问题它工作正常,但对于每一列(row.rowIndex == 0 && cell.cellIndex != 0) 它仅在浏览器 IE 和 FF 中有效,在 Chrome 中无效。它得到一个 rowIndex = -1
for (var i = 0; i <= inpRow.value; i++) {
array[i] = new Array();
arrayFunc[i] = new Array();
row = document.createElement('tr')
table.appendChild(row);
for (var j = 0; j <= inpCol.value; j++) {
cell = document.createElement('td');
row.appendChild(cell);
cell.setAttribute('id', (i) + "." + (j));
if (row.rowIndex == 0 && cell.cellIndex != 0) { // row.rowIndex in chrome gets -1
if ((j-1) >= 26) { // j-1 to asign letter from the second cell
var tmp = (j-1) / 26;
for (var f = 0; f < parseInt(tmp, 10) ; f++) {
for (var k = 0; k <= (j-1) - (26 * parseInt(tmp, 10)) ; k++) {
cell.innerHTML = '<b>' + String.fromCharCode(f + 65) + String.fromCharCode(k + 65) + '</b>';
}
}
} else {
cell.innerHTML = '<b>' + String.fromCharCode(j + 64) + '</b>';
}
cell.style.backgroundColor = 'gray';
} else if (cell.cellIndex == 0) {
cell.innerHTML = '<b>' + i + '</b>';
cell.style.backgroundColor = 'gray';
}
}
我有替代变体,不使用行和单元格索引,而是使用 id:
cell.id.substr(0, 1) == '0' && cell.id != '0.0'
它在所有浏览器中都能正常工作,但我想使用单元格和行索引:
row.rowIndex == 0 && cell.cellIndex != 0