Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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 jquery在for循环中显示未定义的每个函数和数组_Javascript_Jquery_Arrays_Loops_Push - Fatal编程技术网

Javascript jquery在for循环中显示未定义的每个函数和数组

Javascript jquery在for循环中显示未定义的每个函数和数组,javascript,jquery,arrays,loops,push,Javascript,Jquery,Arrays,Loops,Push,这是解决我问题的html部分 <DIV ID="CONTAINER"> <DIV CLASS="ITEMS"> Purchase </DIV> <DIV CLASS="ITEMS"> Return </DIV> <DIV CLASS="ITEMS"> On Hold </DIV> <DIV CLASS="

这是解决我问题的html部分

<DIV ID="CONTAINER">
    <DIV CLASS="ITEMS">
        Purchase
    </DIV>
    <DIV CLASS="ITEMS">
        Return
    </DIV>
    <DIV CLASS="ITEMS">
        On Hold
    </DIV>
    <DIV CLASS="ITEMS">
        Exchange
    </DIV>
</DIV>    

购买
返回
暂停
交换
Jquery

var MyArray = [];  
var $Items = $('.ITEMS');
$Items.each( 
  function(){
    Value = $.trim($(this).html());
    MyArray.push(Value);
  }
);
var Count_Parts = MyArray.length;            
for (i = 1; i <= Count_Parts; i++ ){
  console.log(MyArray[i]);
}
var MyArray=[];
变量$Items=$('.Items');
$Items。每个(
函数(){
Value=$.trim($(this.html());
push(值);
}
);
var Count_Parts=MyArray.length;

对于(i=1;i数组索引从0开始…(长度-1)

var Count\u Parts=MyArray.length;
对于(i=0;i

演示:

您的代码大部分是正确的,它将所有项目的正确值推送到数组中。 您正在使用的for循环并没有像您预期的那样进行迭代。如果您查看这一行:

for (i = 1; i <= Count_Parts; i++ ){

for(i=1;i for您的循环…将其更改为
for(i=0;i
数组是零索引的…”“感谢您如此详细地解释,我希望您在允许的情况下给出答案
for (i = 1; i <= Count_Parts; i++ ){
for (i = 0; i < Count_Parts; i++ ){
var MyArray = [];  
var $Items = $('.ITEMS');
$Items.each( 
  function(){
    Value = $.trim($(this).html());
    MyArray.push(Value);
  }
);
var Count_Parts = MyArray.length;            
for (i = 0; i < Count_Parts; i++ ){
  console.log(MyArray[i]);
}