2013-10-03 50 views
2

这是这篇文章的继续问题: Add style to random loaded divs我现在试图尽可能简化这个问题。添加样式随机加载divs - 不能正常工作

这里所说:

使用此代码我试图添加样式随机加载取决于在它们被加载什么顺序的项目。

<script> 
$(document).ready(function(){ 
var divs = $("div.item").get().sort(function(){ 
return Math.round(Math.random())-0.5; 
}).slice(0,6) 

$(divs).each(function(index) { 
if(index==1 || index==3) 
$(this).css("margin-left", "0%"); 
else 
$(this).css("margin-left", "2%"); //or whatever left value you need 
}); 
$(divs).show(); 
}); 
</script> 

我需要.item条排队在这幅画你刷新浏览器每隔多少时间 enter image description here 到目前为止,这只是ocurs偶然。

我觉得如果你自己尝试一下,你会看到这个问题 这里是一个快速的复制/粘贴整个事情

<html> 
    <head> 


    <script src="http://code.jquery.com/jquery-1.8.2.js"></script> 

    <style> 
    .container {width:750px; background-color:#CCC; height:200px; padding-top:70px; margin: 0 auto; margin-top:5%} 
    .item {display:none; text-align:center; width:32%; float:left} 
    </style>  

    <script> 
    $(document).ready(function(){ 
    var divs = $("div.item").get().sort(function(){ 
    return Math.round(Math.random())-0.5; 
    }).slice(0,6) 

    $(divs).each(function(index) { 
     if(index==1 || index==3) 
      $(this).css("margin-left", "0%"); 
     else 
      $(this).css("margin-left", "2%"); //or whatever left value you need 
    }); 
    $(divs).show(); 
    }); 
    </script> 

    </head> 

    <body>  

     <div class="container"> 
      <div class="item" style="background-color:#F00">1</div> 
      <div class="item" style="background-color:#9F0">2</div> 
      <div class="item" style="background-color:#FF0">3</div> 

      <div class="item" style="background-color:#939">4</div> 
      <div class="item" style="background-color:#3CF">5</div> 
      <div class="item" style="background-color:#CF3">6</div> 

      <div class="item" style="background-color:#6C9">7</div> 
      <div class="item" style="background-color:#999">8</div> 
      <div class="item" style="background-color:#90F">9</div> 

      <div class="item" style="background-color:#FF9">10</div> 
      <div class="item" style="background-color:#099">11</div> 
      <div class="item" style="background-color:#666">12</div> 

      </div> 



    </body> 
    </html> 
+2

这是怎么回事? – David

回答

1

这是因为你在循环的div不会总是匹配标记您的div的顺序,这意味着你会应用了错误的利润率。试试下面的代码:

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.8.2.js"></script> 
    <style> 
     .container {width:750px; background-color:#CCC; height:200px; padding-top:70px; margin: 0 auto; margin-top:5%} 
     .item {display:none; text-align:center; width:32%; float:left} 
    </style>  

    <script>   
     $(document).ready(function(){ 
      var $container = $('div.container'), 
       divs = $("div.item").get().sort(function(){ 
        return Math.round(Math.random())-0.5; 
       }).slice(0,6), 

       <!-- Make a clone, leaving original pot untouched --> 
       $clonedDivs = $(divs).clone(); 


      <!-- Clear container --> 
      $container.html(''); 

      <!-- Append new divs to container --> 
      $clonedDivs.each(function(index) { 
       $container.append(this); 

       if (index % 3 == 0) { 
        $(this).css("margin-left", "0%"); 
       } else { 
        $(this).css("margin-left", "2%"); //or whatever left value you need 
       } 
      }); 

      $clonedDivs.show(); 
     }); 
    </script> 
</head> 
<body> 
    <div class="pot"> 
     <div class="item" style="background-color:#F00">1</div> 
     <div class="item" style="background-color:#9F0">2</div> 
     <div class="item" style="background-color:#FF0">3</div> 

     <div class="item" style="background-color:#939">4</div> 
     <div class="item" style="background-color:#3CF">5</div> 
     <div class="item" style="background-color:#CF3">6</div> 

     <div class="item" style="background-color:#6C9">7</div> 
     <div class="item" style="background-color:#999">8</div> 
     <div class="item" style="background-color:#90F">9</div> 

     <div class="item" style="background-color:#FF9">10</div> 
     <div class="item" style="background-color:#099">11</div> 
     <div class="item" style="background-color:#666">12</div>  
    </div> 

    <div class="container"> 
    </div> 
</body> 
</html> 
+0

另外,是否有可能从外部html加载div.item的? – no0ne

2

因为你是不是随机的DOM 顺序,只有什么divs包含在divs数组中。订单仍然是数字。

因此,当使用$.each(divs)循环divs时,您正在循环所创建的随机顺序,但是DOM顺序仍未触及(如果有意义的话)。你可以说divs$('div.items')不同步。

你可以试试这个:(DEMO:http://jsbin.com/aSejiWA/3

$(document).ready(function(){ 
    var divs = $("div.item").get().sort(function(){ 
     return Math.round(Math.random())-0.5; 
    }).slice(0,6); 

    $(divs).addClass('show'); // to re-select the visual items 

    $('.item.show').each(function(index) { 
     $(this).css('margin-left', index%3 ? '2%' : 0); 
    }).show(); 
}); 
+0

谢谢大卫! – no0ne