jQuery 追加 div

发布于 2024-11-09 01:18:37 字数 771 浏览 0 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2024-11-16 01:18:37
var colors = [ 'red', 'blue', 'green', '#ffe' ];
...
for (var i = 0; i < cell_width_num * cell_height_num; i++) {
    var color = colors[Math.floor(Math.random() * colors.length)];
    $('#container').append(
        $('<div/>', {
            style: 'background-color:' + color,
            text: 'some text'
        })
    );
}

另请注意,颜色数组中包含的颜色不是有效的 CSS 颜色名称。您可以使用 #RGB 等效项来定义它们。

var colors = [ 'red', 'blue', 'green', '#ffe' ];
...
for (var i = 0; i < cell_width_num * cell_height_num; i++) {
    var color = colors[Math.floor(Math.random() * colors.length)];
    $('#container').append(
        $('<div/>', {
            style: 'background-color:' + color,
            text: 'some text'
        })
    );
}

Also note that colors contained in the colors array are not valid CSS color names. You could use the #RGB equivalent to define them.

蹲在坟头点根烟 2024-11-16 01:18:37

这更适合

Live example

代码:

/* Get the cell dimentions and populate the grid */
var cell_height_num = 50; /* This is equal to 50 */
var cell_width_num = 50; /* This is also equal to 50 */
for (var i = 0; i < cell_height_num; i++) {
    var tr = $("<tr></tr>").appendTo(container);
    for (var j = 0; j < cell_width_num; j++) {
        tr.append($("<td></td>", {
            css: {
                width: "1px",
                height: "1px",
                backgroundColor: colors[Math.floor(Math.random() * colors.length)]
            }
        }));
    }
}

This is better suited to a <table>

Live example

code:

/* Get the cell dimentions and populate the grid */
var cell_height_num = 50; /* This is equal to 50 */
var cell_width_num = 50; /* This is also equal to 50 */
for (var i = 0; i < cell_height_num; i++) {
    var tr = $("<tr></tr>").appendTo(container);
    for (var j = 0; j < cell_width_num; j++) {
        tr.append($("<td></td>", {
            css: {
                width: "1px",
                height: "1px",
                backgroundColor: colors[Math.floor(Math.random() * colors.length)]
            }
        }));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文