Javascript 图像未正确填充在 HTML 表中
我正在学习如何将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设
imageA
和imageB
是图像文件的路径。如果是这样,您的问题就在这里:您将新创建的元素 object 分配给
img
,然后重新分配一个 string (图像的路径) ) 到同一个变量。相反,您必须向您创建的
img
元素添加一个 source 属性,并将其值设置为图像的路径:然后您可以附加
img
元素,完成及其src
属性到单元格。I'm assuming
imageA
andimageB
are paths to image files. If so, your problem is here: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:You can then append the
img
element, complete with itssrc
attribute, to the cell.