Javascript 图像未正确填充在 HTML 表中

发布于 2025-01-13 05:46:22 字数 1427 浏览 5 评论 0原文

我正在学习如何将 JavaScript 合并到 HTML 中,并尝试基于课堂项目构建一个非常简单的网页游戏。 (这是一个侧边栏,我成功地完成了项目本身。)

我需要两个特定的图像来随机填充整个表格。我的代码成功创建了适当长度的数组并将其正确转换为 HTML,但图像源似乎没有正确填充。每个图像总是显示一张图像,因此我确信我的源路径是正确的,并且其中一张图像始终位于右下角,但该字段的其余部分是空白的。在检查 DevTools 时,该数组具有适当的 img 属性,但没有源,显然除了具有可见图像的 img 之外。

class Field {

    static generateField(h, w, pct = .2) {
        let board = [];
        let tempArray = [];
        while (board.length < h) {
            while (tempArray.length < w) {
                const random = Math.random();
                if (random < pct) {
                    tempArray.push(imageB);
                } else {
                    tempArray.push(imageA);
                }
            }
            board.push(tempArray);
        }

        for (let a = 0; a < h; a++) {
            let row = document.createElement("tr");
            for (let b = 0; b < w; b++) {
                let cell = document.createElement("td");
                let img = document.createElement("img");
                img = board[a][b];
                img.style.width = "100%";
                img.style.height = "100%";
                cell.appendChild(img);
                row.appendChild(cell);
            }
            tblBody[0].appendChild(row);
        }
    }
}

tblBody 是在class 之外定义的。最初,我的代码是嵌套的 for 循环,并给出了完全相同的结果。

我哪里错了?

I am learning how to incorporate JavaScript into HTML and am trying to build a very simple web game based off a class project. (This is a sidebar, I successfully completed the project itself.)

I need two specific images to randomly populate the entire table. My code successfully creates the array of appropriate length and translates that correctly to the HTML, but the image sources don't seem to populate correctly. One image of each always shows up so I am confident my source paths are correct, and one of the images is always in the lower right hand corner, but the rest of the field is blank. When examining DevTools the array is there with appropriate img attributes but there is no source, except obviously on the img's that have a visible image.

class Field {

    static generateField(h, w, pct = .2) {
        let board = [];
        let tempArray = [];
        while (board.length < h) {
            while (tempArray.length < w) {
                const random = Math.random();
                if (random < pct) {
                    tempArray.push(imageB);
                } else {
                    tempArray.push(imageA);
                }
            }
            board.push(tempArray);
        }

        for (let a = 0; a < h; a++) {
            let row = document.createElement("tr");
            for (let b = 0; b < w; b++) {
                let cell = document.createElement("td");
                let img = document.createElement("img");
                img = board[a][b];
                img.style.width = "100%";
                img.style.height = "100%";
                cell.appendChild(img);
                row.appendChild(cell);
            }
            tblBody[0].appendChild(row);
        }
    }
}

The tblBody is defined outside the class. Originally my code was nested for loops and gave the exact same result.

Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

非要怀念 2025-01-20 05:46:22

我假设 imageAimageB 是图像文件的路径。如果是这样,您的问题就在这里:

let img = document.createElement("img");
img = board[a][b];

您将新创建的元素 object 分配给 img,然后重新分配一个 string (图像的路径) ) 到同一个变量。

相反,您必须向您创建的 img 元素添加一个 source 属性,并将其值设置为图像的路径:

let img = document.createElement("img");
img.setAttribute("src", "<path to imageB>");

然后您可以附加 img 元素,完成及其 src 属性到单元格。

I'm assuming imageA and imageB are paths to image files. If so, your problem is here:

let img = document.createElement("img");
img = board[a][b];

You are assigning a newly created element object to img and then reassigning a string (the path to the image) to the same variable.

Instead, you have to add a source attribute to the img element you created, and set its value to the path for the image:

let img = document.createElement("img");
img.setAttribute("src", "<path to imageB>");

You can then append the img element, complete with its src attribute, to the cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文