【发布时间】:2022-07-04 16:51:17
【问题描述】:
我正在尝试做一个吃豆人游戏,我在棋盘上添加布局的函数代码如下所示:
for (let i = 0; i < layout.length; i++) {
const square = document.createElement('div')
grid.appendChild(square)
squares.push(square)
// add layout to the board //
if (layout[i] === 0) {
squares[i].classList.add('pac-dot')
} else if (layout[i] === 1) {
squares[i].classList.add('wall')
} else if (layout[i] === 3) {
squares[i].classList.add('strawberry')
}
}
我想将图像添加到 classList Strawberry 中的正方形中,我尝试在 html 上创建一个 div 并使用 css 对其进行样式设置,但没有成功。我还尝试了带有 id 的 img 标签,但也没有成功。
如何将图像添加到正方形?
感谢您的帮助!
【问题讨论】:
-
const square = new Image(); square.src = 'url/to/your/image.png'; -
那么DIV的样式有哪些。如果它没有显示图像,这似乎是你的问题。显示您的 CSS。
-
看起来像这样 ` div.image { background-image: url("");背景重复:不重复;宽度:10px;高度:20px;边框半径:10px; } `
标签: javascript html css