Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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'];

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
262 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...