我正在製作一種畫廊,目的應該是,如果您點擊一個圖像,並在屏幕上顯示一個較大的圖像。有了這個圖片,它應該是一個標題和一個文本。我的問題是,我怎樣才能找出哪一個被點擊,以便正確的圖像和正確的文字和標題顯示?找出使用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