任何人都可以告诉我代码有什么问题

时间:2012-12-26 23:24:38

标签: javascript

function MoveAddToCartAccordingly()
{
  var elems = document.getElementsByClassName('box-collateral box-related');
  var av = document.getElementsByClassName('availability in-stock');
  var sp = document.getElementsByClassName('product-options');
  var ac = document.getElementsByClassName('add-to-cart');
  var first = document.getElementsByClassName('item first');
  var second = document.getElementsByClassName('item');
  for(var k = 0; k < sp.length; k++){
   if (getComputedStyle(sp[k]).visibility == "visible")
   {  
     for (var i = 0; i < elems.length; i++) {
      if (getComputedStyle(elems[i]).visibility == 'visible') {
       for (var j = 0; j < av.length; j++) {
        av[j].style.visibility = 'visible';
        av[j].id = "someID";
        elems[i].appendChild(av[j]);
       }     
      }
      else
      {  
        for (var s = 0; s < av.length; s++) {
         av[s].style.visibility = 'hidden';
        }
        for (var l = 0; l < ac.length; l++) {
         ac[l].style.marginTop = "-500px";
         ac[l].style.marginLeft = "-20px";
        }
      }
     }

   }
   return;
  }
  for (var p = 0; p < elems.length; p++) {
   if (getComputedStyle(elems[p]).visibility == 'visible') {
     for (var q = 0; q < av.length; q++) {
        av[q].style.visibility = 'visible';
        av[q].id = "someID";
        elems[p].appendChild(av[q]);
     }     
     if(elems[p].style.marginTop == "-610px")
     { 
        elems[p].style.marginTop = "-640px";
     }
     for(var r = 0; r < first.length; r++)
     {
       if(getComputedStyle(first[r]).visiblity == 'visible'){
        for(var m = 0; m < ac.length; m++)
        { 
          if(ac[m].style.marginTop == "-120px")
          {
            ac[m].style.marginTop ="-140px";
          }   
        } 
       }
       else if(getComputedStyle(first[r]).visiblity == 'visible' && getComputedStyle(second[r]).visiblity == 'visible' )
       {
         for(var n = 0; n < ac.length; n++)
         { 
           if(ac[n].style.marginTop == "-120px")
          {
            ac[n].style.marginTop ="-140px";
          }
         }
       } 
     }
   }
 }
}
window.onload = MoveAddToCartAccordingly;

任何人都可以在这里找到错误的代码实际上我正在检查div产品选项是否可见然后我再次检查是否div与类“box-colatral box-related”如果可见如果不是那么我隐藏其他标签p与类可用库存和移动添加到购物车div到顶部位置,但那个不工作

1 个答案:

答案 0 :(得分:3)

你的第一个for循环中有一个return语句。我想你在该循环的第一次迭代之后总是会在函数调用上保释。