English 中文(简体)
Code don t work, can t read property className of undefined
原标题:

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.

最佳回答

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.

问题回答

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.

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++;
}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签