1

我希望为单元格的第一行和第一列分配一个字母和一个数字(如 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

完整代码

4

2 回答 2

0

似乎 Chrome 和 Safari 都为使用“document.createElement('tr');”附加到表的每一行分配了 -1 的行索引。方法。然后没有必要按索引搜索或比较行,因为每一行只是-1。

而不是在第二个 for 循环开始之前的以下 2 行:

row = document.createElement('tr')
table.appendChild(row);

利用:

row = table.insertRow(-1);

这应该做完全相同的工作,但 Chrome 和 Safari 现在将为每一行插入正确的索引。

于 2014-03-01T23:37:46.847 回答
0

这是Webkit布局引擎中的一个错误(也转移到了分叉的Blink引擎)。这就是为什么它在 Firefox 中运行良好但在 Chrome (Blink) 或 Safari (Webkit) 中运行良好的原因。

此处报告了该错误。

于 2015-06-19T17:08:58.603 回答