有没有办法在js中设置单个数组元素的样式 [英] Is there a way to style individual array elements in js
问题描述
我目前正在开发一个记忆游戏,我在游戏板上填充了通过 js
创建的 div.我正在尝试将图像添加到我的数组值中.这是我的数组:
I am currently working on a memory game where I have a game board populated with divs created through js
. I'm trying to add images to my array values.
Here is my array:
var memory_array = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F', 'G', 'G', 'H', 'H'];
我想为每对附加一张图片,以使我的项目更有趣.我目前正在尝试memory_array[0].style.background-image
它破坏了我的项目.任何帮助将不胜感激.
I would like to attach an image to each pair to make my project more interesting. I am currently trying
memory_array[0].style.background-image
and it is breaking my project. Any help would be greatly appreciated.
推荐答案
它是 .backgroundImage
,但是你的数组由 char 值组成,它们没有 style
.要调用 .style
,您必须获得 DOM
元素,例如使用 document.getElementById
或 document.getElementsByClassName
.我建议给他们类名 (.className
) 并在 css 中设置图像,而不是直接使用 .style.backgroundImage
.
It's .backgroundImage
, but your array consists of char values, they don't have the property style
. For calling .style
, you have to get the DOM
element, f.e. with document.getElementById
or document.getElementsByClassName
. I would recommend to give them class names (.className
) and setting the images in css instead of using .style.backgroundImage
directly.
这篇关于有没有办法在js中设置单个数组元素的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!