代码不起作用,无法读取属性“className”;未定义的

发布于 2024-09-01 21:08:20 字数 871 浏览 9 评论 0原文

这段代码有什么问题?

var divarray = [];
var articleHTML = [];
var absHTML;
var keyHTML;
var bodyHTML = [];
var i = 0;
divarray = document.getElementById("yui-main").getElementsByTagName("div");
for ( var j in divarray) {
    if(divarray[i].className == "articleBody"){
        articleHTML = divarray[i];
        for( var k in articleHTML ){
            bodyHTML[i] = '';
            if(articleHTML[i].className == "issueMiniFeature"){continue;}
            if(articleHTML[i].className == "abstract"){absHTML = articleHTML[i]; continue;}
            if(articleHTML[i].className == "journalKeywords"){keyHTML = articleHTML[i]; continue;}
            bodyHTML[i] = articleHTML[i];
        }
        break;
    }
    i++;
}

我收到的错误是:

TypeError: Cannot read property 'className' of undefined

我正在使用 Google Chrome。

What is wrong with this code?

var divarray = [];
var articleHTML = [];
var absHTML;
var keyHTML;
var bodyHTML = [];
var i = 0;
divarray = document.getElementById("yui-main").getElementsByTagName("div");
for ( var j in divarray) {
    if(divarray[i].className == "articleBody"){
        articleHTML = divarray[i];
        for( var k in articleHTML ){
            bodyHTML[i] = '';
            if(articleHTML[i].className == "issueMiniFeature"){continue;}
            if(articleHTML[i].className == "abstract"){absHTML = articleHTML[i]; continue;}
            if(articleHTML[i].className == "journalKeywords"){keyHTML = articleHTML[i]; continue;}
            bodyHTML[i] = articleHTML[i];
        }
        break;
    }
    i++;
}

The error I get is:

TypeError: Cannot read property 'className' of undefined

I am using Google Chrome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守不住的情 2024-09-08 21:08:20

你正在使用一个非常奇怪的循环。

 for (var j in array) {
    // use array[i]
    ++ i
 }

问题是 .getElementsByTagName 不返回数组,而是返回类似数组的接口。例如:

>>> for (var j in document.getElementsByTagName('body')) console.log(j)
0
length
item
namedItem

因此,在 for/in 循环中,i 将上升到 array.length + 2而不是array.length - 1。由于 array[array.length + 2] 不存在,因此当您尝试访问它的属性时,它将返回 undefined 并抛出错误。


总是

for (var i = 0, len = array.length; i < len; ++ i) {
  ...
}

与数组一起使用。

You're using a very strange loop.

 for (var j in array) {
    // use array[i]
    ++ i
 }

The problem is .getElementsByTagName doesn't return an array, but an array-like interface. For example:

>>> for (var j in document.getElementsByTagName('body')) console.log(j)
0
length
item
namedItem

Therefore, in your for/in loop, i will go up to array.length + 2 instead of array.length - 1. Since array[array.length + 2] does not exist, it will return undefined and throw the error when you try to access a property of it.


Just always use

for (var i = 0, len = array.length; i < len; ++ i) {
  ...
}

with arrays.

燕归巢 2024-09-08 21:08:20

您将 articleHTML 初始化为数组,然后显然将 articleHTML 设置为 HTMLElement (divarray[i]),然后将其视为array (articleHTML[i]) — 这实际上试图获取您从 divarray 中提取的 HTMLElement 的 i 属性,但它不会不存在,然后你尝试获取这个未定义值的className

You initialize articleHTML as an array, then you're apparently setting articleHTML to an HTMLElement (divarray[i]) but then treating it like an array (articleHTML[i]) — this actually tries to get the i property of the HTMLElement that you pulled out of divarray, which doesn't exist, and then you try to get the className of this undefined value.

半寸时光 2024-09-08 21:08:20

你的意思:

var divarray = [];
var articleHTML = [];
var absHTML;
var keyHTML;
var bodyHTML = [];
var i = 0;
divarray = document.getElementById("yui-main").getElementsByTagName("div");
for ( var j in divarray) {
    if(divarray[j].className == "articleBody"){
        alert("found");
        articleHTML = divarray[j];
        break;
    }
    bodyHTML[i] = '';
    if(divarray[j].className == "issueMiniFeature"){continue;}
    if(divarray[j].className == "abstract"){absHTML = divarray[j]; continue;}
    if(divarray[j].className == "journalKeywords"){keyHTML = divarray[j]; continue;}
    bodyHTML[i] = divarray[j];
    i++;
}

Did you mean:

var divarray = [];
var articleHTML = [];
var absHTML;
var keyHTML;
var bodyHTML = [];
var i = 0;
divarray = document.getElementById("yui-main").getElementsByTagName("div");
for ( var j in divarray) {
    if(divarray[j].className == "articleBody"){
        alert("found");
        articleHTML = divarray[j];
        break;
    }
    bodyHTML[i] = '';
    if(divarray[j].className == "issueMiniFeature"){continue;}
    if(divarray[j].className == "abstract"){absHTML = divarray[j]; continue;}
    if(divarray[j].className == "journalKeywords"){keyHTML = divarray[j]; continue;}
    bodyHTML[i] = divarray[j];
    i++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文