0

我正在尝试完成以下任务:

1) for 循环遍历产品列表,在每个产品上调用 requestCrossDomain() 函数。

2) 函数 requestCrossDomain() 会将检索到的每个产品的 3 组属性/描述传递回 requestCross Domain() 函数。

3)requestCrossDomain()函数完成后,使用闭环函数遍历属性/描述结果,创建3个数组。重要的是要知道这个函数一次只能在一个产品上工作,这意味着如果 for 循环在 product[0] 处,那么回调函数也应该在 product[0] 的属性/描述集上工作。

4) 使用 if() 语句查找匹配集,并返回数组/对象的索引位置。

我坚持使用 3),循环关闭似乎没有在 console.log 中返回任何内容。

//step 1)
for (i=0; i<product.length; i++){
   ....       
   requestCrossDomain(arg[i], function(i) {   //step 3)
                                              //need to hold the i value 
      return function() {                     //so the return function() is working on 
         var array = [];                      //the matching product[i]
         $('h4').each(function(j) { 
            array[j] = [];
            var $this = $(this);
            array[i][j] = {
               Attribute: $this.text(),
               Description: $this.next('p').text()
            };

            //step 4)
            if($this.text() == "Attr" && $this.next('p').text() == "Desc") {  
                console.log(i + "-" +j);  //nothing return, no error message
            };
         });          
      }; 

   }(i));
}

//step 2)
function requestCrossDomain(arg, callback) {
   ....
   // 3 sets of attribute/description are constructed here, the html will look like:
   // <div>
   //   <h4>Attribute</h4>
   //   <p>Description</p>
   //   <h4>Attr</h4>
   //   <p>Desc</p>
   //   <h4>A</h4>
   //   <p>D</p>
   // </div>    
   ....
   callback();
}

我猜我没有完全理解 for 循环的回调函数。我的头已经旋转了几个小时,有人可以发光吗?谢谢!

4

1 回答 1

0

我相信你的数组没有正确初始化。首先,你的意思是把

array[i] = []; //instead of array[j] right after $('h4').each(function(j) {

? 因为它使您的代码更有意义。如果你改变它然后添加

array[i][j] = [];

作为下一行,我相信你应该很高兴。让我知道。

另外,尝试放置 console.log("Step X"); 您在任何地方都评论了一个步骤以确保达到 tat 步骤,并查看是否所有步骤都已达到/哪个步骤中断。

于 2013-02-03T17:12:26.683 回答