我正在制作一种画廊,目的应该是,如果您点击一个图像,并在屏幕上显示一个较大的图像。有了这个图片,它应该是一个标题和一个文本。我的问题是,我怎样才能找出哪一个被点击,以便正确的图像和正确的文字和标题显示?找出使用javascript/jQuery点击了哪个图像
通过使用this.src
我能够找到图像源,但找不到从那里走的路。不知道这是否是正确的轨道上...
无论如何。这里是我的代码到目前为止:
$(document).ready(function() {
var gallery = [
{
"img" : "img/gallery/gameboy2.png",
"title" : "GameBoy Color",
"about" : "A-ball",
"price" : 99,
"category" : ["gameboy", "color", "console", "game"]
},
{
"img" : "img/gallery/phone.png",
"title" : "Hamburger Phone",
"about" : "What is a smartphone?",
"price" : 129,
"category" : ["phone","hamburger"]
},
{
"img" : "img/gallery/gameboy.png",
"title" : "Nintendo GameBoy",
"about" : "Things doesnt get better with time. This is the shit.",
"price" : 499,
"category" : ["game","console", "gameboy"]
},
{
"img" : "img/gallery/game2.png",
"title" : "SEGA",
"about" : "Things doesnt get better with time. This is the shit.",
"price" : 699,
"category" : ["game","console", "sega"]
},
{
"img" : "img/gallery/gameboy2.png",
"title" : "GameBoy Color",
"about" : "A-ball",
"price" : 99,
"category" : ["gameboy", "color", "console", "game"]
},
{
"img" : "img/gallery/phone.png",
"title" : "Hamburger Phone",
"about" : "What is a smartphone?",
"price" : 129,
"category" : ["phone","hamburger"]
},
{
"img" : "img/gallery/gameboy.png",
"title" : "Nintendo GameBoy",
"about" : "Things doesnt get better with time. This is the shit.",
"price" : 499,
"category" : ["game","console", "gameboy"]
},
{
"img" : "img/gallery/game2.png",
"title" : "SEGA",
"about" : "Things doesnt get better with time. This is the shit.",
"price" : 699,
"category" : ["game","console", "sega"]
}
];
var submit_search = document.getElementById("submit_search");
submit_search.addEventListener("click", searchItem);
showItemsList(gallery);
/*
Create a li element and append to already existing ul
Show an image of the product, and below the image show product title and price
*/
function showItemsList(gallery) {
var ul = document.getElementById("product_list");
ul.innerHTML = "";
for(i =0; i < gallery.length; i++) {
// get the current product
var currentProduct = gallery[i];
// create element li
var li = document.createElement('li');
// create product img
var productImg = document.createElement('img');
productImg.src = currentProduct.img;
productImg.className = "thumbnail";
productImg.addEventListener("click", showItem);
li.appendChild(productImg);
// create caption
li.appendChild(document.createTextNode(currentProduct.title + " " + currentProduct.price + "kr"));
ul.appendChild(li);
}
}
/*
If someone click on a product, show a larger picture with a caption telling about the product
If you click the picture again, it minimize back to a thumbnail
*/
function showItem() {
// display the layer
document.getElementById("overlay").style.display = "block";
// add the name of the product
var h2 = document.getElementById("header_products");
var single_item_page = document.getElementById("single_item");
h2.appendChild(document.createTextNode("Hej"));
}
当你添加任何元素到页面中,您可以附加对该元素进行“onClick”回调。 onClick中提到的功能将在单击该项目时被调用。通过onClick传递的事件将识别哪个元素被点击。 – Kolban 2014-10-30 14:32:49
我已经使用“addEventListener”而不是onClick,听说它是一个更好的方法。 – teninchhero 2014-10-30 14:39:34