Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:无法读取属性';innerHTML';未定义的。为什么?_Javascript_Html_Innerhtml_Typeerror - Fatal编程技术网

Javascript 未捕获类型错误:无法读取属性';innerHTML';未定义的。为什么?

Javascript 未捕获类型错误:无法读取属性';innerHTML';未定义的。为什么?,javascript,html,innerhtml,typeerror,Javascript,Html,Innerhtml,Typeerror,我有一个简单的函数,它从现有表中检索一个值。每次我的应用程序运行此函数时,我都会得到uncaughttypeerror:无法读取未定义的属性“innerHTML”error。功能是: function getDataCapData(proc, index){ var cd=document.getElementById("capacityD"); var i=2; while(i<cd.rows.length){ if(proc.localeComp

我有一个简单的函数,它从现有表中检索一个值。每次我的应用程序运行此函数时,我都会得到
uncaughttypeerror:无法读取未定义的属性“innerHTML”
error。功能是:

function getDataCapData(proc, index){
    var cd=document.getElementById("capacityD");
    var i=2;
    while(i<cd.rows.length){
        if(proc.localeCompare(cd.rows[i].cells[0].innerHTML)==0) return 1*cd.rows[i].cells[index].innerHTML;
        i++;
    }
    return 0;
}

在while语句中放入调试器。然后用f12打开控制台。之后,使用f10,您可以一步一步地完成代码。在对象
cd
的某处,
行[i]。单元格[0]
元素未定义。这就是为什么会出现此错误。

Test
proc.localeCompare(cd.rows[i].cells[0]
console.log
中可以显示html吗?@Rayon,我已经测试了localeCompare()函数,它可以正常工作。因此,未定义的值就是返回值?@JimmyPage,请共享一个Fiddle/可执行的演示文件以进行测试。能否将返回值更改为return(1*cd.rows[i].cells[0].innerHTML)看看错误是否消失了var index的值是多少?可能是超出范围的值。我发誓localeCompare总是正常工作。我认为错误在返回值中,事实上这个函数总是返回1。@JimmyPage事实上,大多数浏览器都会告诉你确切的行(通常,甚至是确切的列)。您使用的浏览器是什么?我使用的是Chrome nowLine number,它位于错误消息的另一侧,可能会在一个宽窗口()中被忽略。然后,您可以单击文件名将其发送到或多或少的错误位置()。我更习惯于Firefox和Firebug,我觉得这更直观()。
function getDataCapData(proc, index){
    var cd=document.getElementById("capacityD");
    var i=2;
    while(i<cd.rows.length && proc.localeCompare(cd.rows[i].cells[0].innerHTML)!=0) i++;
    return( 1*cd.rows[i].cells[index].innerHTML);
}