简体   繁体   中英

How would I get output from the pop-up images thats being clicked in Javascript?

I have an array of images where they randomly come up on the screen, and the user is required to click on the images that applies to them. Now what I need is How would I monitor which image they clicked so as the developer I know which images apply to them? I can also make something like a list of images they clicked on the bottom of the screen.

Would anyone show me directions on how would I go on doing this because I don't have a clue to where to start from. Thank you

This is how I generated the random images

var imageArray = new Array(); 
imageArray[0] = new Image(); 
imageArray[0].src = "football.png"; 
imageArray[1] = new Image(); 
imageArray[1].src = "painting.png"; 
var randomImage = imageArray[Math.floor(Math.random() * imageArray.length)]; 
var hobby = new Bitmap(randomImage);

What I would do is give each of the images an ID, I would probably give them numbers.

Then adding this to your script for each image.

document.getElementById(insert image id here).onclick = function() {
    //Enter what happens when the user clicks the image here. 
};

You can also do it using jquery like so:

$("#insert image ID").click(function(){
    //Enter what happens when the user clicks the image here.
});

There is probably a better way to do this, but I cant really imagine it right now :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM