我想將div附加到父div,每個較小的div是從數組中選擇的隨機背景顏色。我會怎麼做?jQuery append divs
我已經有了:
$(document).ready(function(){
var cell_size = 10;
var container = $("#container");
var colors = ["limepulp", "goldgreen", "chromeoxidegreen", "kermit", "pear"];
/* Get the cell dimentions and populate the grid */
var cell_height_num = container.height()/cell_size; /* This is equal to 50 */
var cell_width_num = container.width()/cell_size; /* This is also equal to 50 */
for (var i = 0; i < cell_width_num * cell_height_num; i++){
/* What goes here? How can I generate a div with a random background comming from colors[]? */
/* In total, 2500 divs should be generated inside $("#container"); */
}
});
這在某些機器上會很慢... 2500個div很多。我認爲你應該考慮使用'帆布' – Hogan 2011-05-21 13:48:47
這是一個div的crapload。這可能更適合服務器端... – 2011-05-21 13:48:57
@Hogan大約需要200ms來呈現給我。 – Raynos 2011-05-21 14:02:23