简体   繁体   English

javascript:TypeError:document.getElementById(“ myTable”)为null

[英]javascript: TypeError: document.getElementById(“myTable”) is null

i'm getting the following error: 我收到以下错误:

TypeError: document.getElementById("myTable") is null

on document.write(document.getElementById("myTable").rows[i].innerHTML); 在document.write(document.getElementById(“ myTable”)。rows [i] .innerHTML)上; It goes into a never ending loop. 它进入了一个永无止境的循环。

Can someone tell me on what i'm doing wrong? 有人可以告诉我我做错了什么吗? Thank you. 谢谢。

My code: 我的代码:

<script type="text/javascript">
function colorRows(){
count=document.getElementById("myTable").rows.length;
alert("count is:"+count);
            for (i=0;i<=count;i++){
                if (i%2===0){
                    document.write(document.getElementById("myTable").rows[i].innerHTML);
                }
            }
}
</script> 
</head>

<body>
<table id="myTable">

<tr>
    <th>emp_id</th>
    <th>name</th>
    <th>age</th>
</tr>
<tr>
    <td>1</td>
    <td>vaani</td>
    <td>35</td>
</tr>
<tr>
    <td>2</td>
    <td>Ramu</td>
    <td>37</td>
</tr>
<tr>
    <td>3</td>
    <td>Iniyan</td>
    <td>5</td>
</tr>
<tr>
    <td>4</td>
    <td>Mickey</td>
    <td>10</td>
</tr>
</table>
<form method="post" action="JavaScript:colorRows();">
<input type="submit" value="Submit" />
</form>
</body>
</html>

It is because when you do document.write in the loop in the first time you clear dom and there is no more table element, and when you do document.write in the second time then document.getElementById("myTable") provides null . 这是因为,当您第一次在循环中执行document.write时,您会清除dom,并且不再有表格元素;而当您第二次执行document.write时,则document.getElementById("myTable")提供null So browser stops the script provide the error. 因此浏览器停止脚本提供错误。

I have the same error when I run your code. 运行您的代码时出现相同的错误。 I put myTable to a new variable outside of for. 我将myTable放在for之外的新变量中。 It works fine. 工作正常。 like that. 像那样。

var myTableVar = document.getElementById("myTable");
    var i;
    for (i=0;i<=count;i++){
        alert("i = " + i);
        if (i%2===0){
            document.write(myTableVar.rows[i].innerHTML);
        }
    }

Complete code: 完整的代码:

<html>
<head>
<script type="text/javascript">
    function colorRows(){
    count=document.getElementById("myTable").rows.length;
    alert("count is:" + count);
    var myTableVar = document.getElementById("myTable");
    var i;
    for (i=0;i<=count;i++){
        alert("i = " + i);
        if (i%2===0){
            document.write(myTableVar.rows[i].innerHTML);
        }
    }
}
</script> 
</head>

<body>
<table id="myTable">

<tr>
    <th>emp_id</th>
    <th>name</th>
    <th>age</th>
</tr>
<tr>
    <td>1</td>
    <td>vaani</td>
    <td>35</td>
</tr>
<tr>
    <td>2</td>
    <td>Ramu</td>
    <td>37</td>
</tr>
<tr>
    <td>3</td>
    <td>Iniyan</td>
    <td>5</td>
</tr>
<tr>
    <td>4</td>
    <td>Mickey</td>
    <td>10</td>
</tr>
</table>
<form method="post" action="JavaScript:colorRows();">
<input type="submit" value="Submit" />
</form>
</body>
</html>

Try this 尝试这个

function colorRows(){
    var table = document.getElementById("myTable");
    count=table.rows.length;
    alert("count is:"+count);
    for (i=0;i<=count;i++){
        if (i%2===0){
            document.write(table.rows[i].innerHTML);
        }
    }
}

Best to only access the table once anyway. 最好还是只访问一次表。 document.write() clears the page because it calls document.open() document.write()清除页面,因为它调用document.open()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM