0
使用此代碼我試圖隨機化我的響應式網格,這是一個5x3矩陣。我最終得到錯誤追加到一個孩子,但我編輯了我的代碼。基本上,最終的結果,我想是這樣如何洗牌/隨機化引導響應網格?
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
到
10 11 12
13 14 15
7 8 9
1 2 3
4 5 6
格式化網格:
<div id="shuffle">
<div class="text-center">
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4"></div>
<div class="col-sm-4"></div></div>
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4"></div>
<div class="col-sm-4"></div></div>
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4"></div>
<div class="col-sm-4"></div></div>
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4"></div>
<div class="col-sm-4"></div></div>
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4"></div>
<div class="col-sm-4"></div></div>
「正在使用的的jQuery:
function shuffle(tbl) {
var arr = tbl.find("div");
console.log("Finding the arr Value " + arr + " !");
for(
var j, x, i = arr.length; i;
j = parseInt(Math.random() * i),
x = arr[--i], arr[i] = arr[j], arr[j] = x
);
var tmp;
var rows = tbl.find(".col-sm-4").length
console.log("finding the row value " + rows + " !");
//var cols = tbl.find(".col-sm-4:first .row").length
var cols = 3;
console.log("finding the cols value " + cols + " !");
for (i = 0; i < rows; i++){
tmp = tbl.find("col-sm-4").eq(i);
console.log("finding the tmp value " + tmp + " ! ");
tmp.html()
for (j = 0; j < cols; j++)
tmp.append(arr[i*cols+j]);
}
}
jQuery(document).ready(function() {
shuffle(jQuery("#shuffle"));
});
'
個控制檯日誌輸出
Finding the arr Value [object Object] !
xxx/:923 finding the row value 15 !
xxx/:927 finding the cols value 3 !
xxx/:932 finding the tmp value [object Object] !