【问题标题】:Get rowIndex = -1 in Chrome. In IE, FF works fine在 Chrome 中获取 rowIndex = -1。在 IE 中,FF 工作正常
【发布时间】:2017-01-02 01:28:50
【问题描述】:

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

Full code

【问题讨论】:

    标签: google-chrome html-table


    【解决方案1】:

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

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

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

    使用:

    row = table.insertRow(-1);

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

    【讨论】:

      【解决方案2】:

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

      错误报告here

      【讨论】:

        猜你喜欢
        • 2012-03-18
        • 1970-01-01
        • 2012-09-14
        • 2012-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多