无法读取null的属性

无法读取null的属性

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?

(9个答案)


2年前关闭。




他是JavaScript,给我一个错误:“未捕获的TypeError:无法读取null的属性'行'”
var table = document.getElementById("table"),rIndex;

for(var i = 1; i < table.rows.length; i++)
{
    table.rows[i].onclick = function()
    {
        rIndex = this.rowIndex;
        console.log(rIndex);

        document.getElementById("id").value = this.cells[0].innerHTML;
        document.getElementById("nome").value = this.cells[1].innerHTML;
        document.getElementById("morada").value = this.cells[2].innerHTML;
        document.getElementById("localidade").value = this.cells[3].innerHTML;
        document.getElementById("telefone").value = this.cells[4].innerHTML;
        document.getElementById("codpostal").value = this.cells[5].innerHTML;
        document.getElementById("email").value = this.cells[6].innerHTML;
    };
}


// edit the row
function editRow()
{
    table.rows[rIndex].cells[0].innerHTML = document.getElementById("id").value;
    table.rows[rIndex].cells[1].innerHTML = document.getElementById("nome").value;
    table.rows[rIndex].cells[2].innerHTML = document.getElementById("morada").value;
    table.rows[rIndex].cells[3].innerHTML = document.getElementById("localidade").value;
    table.rows[rIndex].cells[4].innerHTML = document.getElementById("telefone").value;
    table.rows[rIndex].cells[5].innerHTML = document.getElementById("codpostal").value;
    table.rows[rIndex].cells[6].innerHTML = document.getElementById("email").value;
}

代码有什么问题?

最佳答案

文档中没有ID为“table”的元素,因此

var table = document.getElementById("table"),rIndex;

表为空。

10-02 12:12