【发布时间】:2022-12-07 11:12:26
【问题描述】:
将其从 JQuery 转换为 javascript 时遇到问题。我试过 document.getElementbyClassName("tile").style.left = x * tileWidth + tileDepth * z + tileOffsetLeft + "px"。但它不喜欢那样并给我错误无法设置未定义的属性(设置“左”)。
export function createTiles() {
for (let counter = 0; counter < coordinates.length; counter++) {
const coord = coordinates[counter];
const [x,y,z] = coord;
const image = images[counter];
const tile = $("<div></div>")
.addClass("tile")
.css({
left: x * tileWidth + tileDepth * z + tileOffsetLeft + "px",
top: y * tileHeight + tileDepth * z + tileOffsetTop + "px",
zIndex: z
});
const tileFront = $("<div></div>")
.addClass("tileFront")
.css({
width: tileWidth + "px",
height: tileHeight + "px",
borderRadius: tileRoundness + "px"
})
.append(image);
tile.append(tileFront).appendTo("#game");
}
}
【问题讨论】:
-
它不喜欢它,因为它应该是
getElementsbyClassName。getElementsbyClassName返回一个您不能直接应用样式的节点列表——您需要遍历节点列表并将样式应用到每个节点。
标签: javascript jquery