Cannot read property *0* of null

前端 未结 4 1642
长发绾君心
长发绾君心 2021-01-13 08:22

I get this error \"Cannot read property \'0\' of null.\"

I have a table with

somename.html

4条回答
  •  长情又很酷
    2021-01-13 09:06

    getElementById returns null if there is no match in the document. (Which then leads to exactly your error message).

    This means that either you have a typo in your selector or the html or the js gets executed before your element is included into the dom.

    Also, getElementById returns a single element and not an array (Node List to be precise), so the correct call would be:

    document.getElementById('td1').style.color = 'blue';
    

    Third problem:

    setInterval(test(),1000);
    //              /\
    // this will immeditately execute the function and
    // hands over the *return value* to setInterval
    

    will not work as intended, it needs to be

    setInterval(test,1000);
    //            /\
    // hand over the *function reference* to be executed after 1 second
    

提交回复
热议问题