jQuery 追加 div
我想将 div 附加到父 div,每个较小的 div 都是从数组中选择的随机背景颜色。我该怎么做呢?
我已经拥有的:
$(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"); */
}
});
I want to append divs to a parent div, each smaller div being a random background color chosen from an array. How would I do so?
What I already have:
$(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"); */
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另请注意,颜色数组中包含的颜色不是有效的 CSS 颜色名称。您可以使用 #RGB 等效项来定义它们。
Also note that colors contained in the colors array are not valid CSS color names. You could use the #RGB equivalent to define them.
这更适合
Live example
代码:
This is better suited to a
<table>
Live example
code: