这个javascript有什么问题?数组未定义

时间:2021-03-23 15:40:30

What's wrong with this code?

这段代码出了什么问题?

var divarray = document.getElementById("yui-main").getElementsByTagName("div");
var articleHTML = array();
var absHTML;
var keyHTML;
var bodyHTML = array();
var i = 0;
for ( var j in divarray) {
    if(divarray[i].className == "articleBody"){
  alert("found");
  articleHTML = divarray[i];
  break;
 }
 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];
 i++;
}

This is the error I am getting:

这是我得到的错误:

ReferenceError: array is not defined

I am using Google Chrome if it helps any.

我正在使用谷歌浏览器,如果它有帮助。

8 个解决方案

#1


21  

It's not php - you should use

它不是PHP - 你应该使用

var variable_name = new Array()

or even better

甚至更好

var variable_name = []

#2


8  

That's not how to declare variables as an empty array. You should be using:

这不是如何将变量声明为空数组。你应该使用:

var articleHTML = [];

See this previous question for reasoning of using this method instead of new Array()

有关使用此方法而不是新Array()的推理,请参阅此前一个问题

#3


2  

It's [] in ECMAScript; this isn't PHP. The interpreter is right - array is not defined, which is why you're getting that.

它是ECMAScript中的[];这不是PHP。解释器是正确的 - 数组没有定义,这就是你得到它的原因。

#4


2  

var articleHTML = new Array();

#5


1  

Instead of

var articleHTML = array();

and

var bodyHTML = array();

do

var articleHTML = [];

and

var bodyHTML = [];

#6


0  

You also don't need to use var six times, you can do:

你也不需要使用var六次,你可以这样做:

var divarray = document.getElementById("yui-main").getElementsByTagName("div"),
    articleHTML = [],
    absHTML = [],
    keyHTML = [],
    bodyHTML = [],
    i = 0;

Which works just as well as your six vars but looks much nicer.

这与你的六个变量一样有效,但看起来更好。

Also there are a number of compelling reasons not to use new in instantiate an array (besides []; is much shorter than new Array();)

还有一些令人信服的理由不在实例化数组时使用new(除了[];比新的Array()要短得多;)

#7


0  

Note! Javascript IS case sensitive you have to use upper-case A in word Array.

注意! Javascript是区分大小写的,你必须在word Array中使用大写A.

var myarr = new array(); //THIS IS WRONG! and will result in error not defined

So these are the correct ways:

所以这些是正确的方法:

var myarr = new Array(); //THIS IS CORRECT (note the "big" A) :)
var myarr = []; //and this is correct too

#8


0  

You first need to define

你首先需要定义

var divarray = new Array(); 

#1


21  

It's not php - you should use

它不是PHP - 你应该使用

var variable_name = new Array()

or even better

甚至更好

var variable_name = []

#2


8  

That's not how to declare variables as an empty array. You should be using:

这不是如何将变量声明为空数组。你应该使用:

var articleHTML = [];

See this previous question for reasoning of using this method instead of new Array()

有关使用此方法而不是新Array()的推理,请参阅此前一个问题

#3


2  

It's [] in ECMAScript; this isn't PHP. The interpreter is right - array is not defined, which is why you're getting that.

它是ECMAScript中的[];这不是PHP。解释器是正确的 - 数组没有定义,这就是你得到它的原因。

#4


2  

var articleHTML = new Array();

#5


1  

Instead of

var articleHTML = array();

and

var bodyHTML = array();

do

var articleHTML = [];

and

var bodyHTML = [];

#6


0  

You also don't need to use var six times, you can do:

你也不需要使用var六次,你可以这样做:

var divarray = document.getElementById("yui-main").getElementsByTagName("div"),
    articleHTML = [],
    absHTML = [],
    keyHTML = [],
    bodyHTML = [],
    i = 0;

Which works just as well as your six vars but looks much nicer.

这与你的六个变量一样有效,但看起来更好。

Also there are a number of compelling reasons not to use new in instantiate an array (besides []; is much shorter than new Array();)

还有一些令人信服的理由不在实例化数组时使用new(除了[];比新的Array()要短得多;)

#7


0  

Note! Javascript IS case sensitive you have to use upper-case A in word Array.

注意! Javascript是区分大小写的,你必须在word Array中使用大写A.

var myarr = new array(); //THIS IS WRONG! and will result in error not defined

So these are the correct ways:

所以这些是正确的方法:

var myarr = new Array(); //THIS IS CORRECT (note the "big" A) :)
var myarr = []; //and this is correct too

#8


0  

You first need to define

你首先需要定义

var divarray = new Array();