类型错误:使用 javascript 删除行时无法读取 null 的属性“行”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20549840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:46:04  来源:igfitidea点击:

Type error: cannot read property 'rows' of null when deleting a row using javascript

javascript

提问by Krishna

I have this javascript

我有这个 javascript

function deleteRow(tableID) 
      {
            try {
            var table = document.getElementById(collct_table_body);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox&& true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }

But when I try to delete a row using this it shows an error "cannot read property 'rows' of null type error"

但是当我尝试使用它删除一行时,它显示错误“无法读取空类型错误的属性‘行’”

Is there any error in this javascript Im using?Can some one help me out?

我使用的这个 javascript 有什么错误吗?有人可以帮我吗?

回答by Krishna

You're missing the quotes

你缺少引号

document.getElementById("collct_table_body");

or, may be this is a variable (which I don't see in your code, global var?? we will never know)

或者,这可能是一个变量(我在你的代码中没有看到,全局变量??我们永远不会知道)

var collct_table_body = "someID";