使用javascript/jQuery查找已单击的图像
Finding out which image that have been clicked on using 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"));
}
试试这个,
$('ul li').click(function(){
var src = $(this).find('img').attr('src');
var keepGoing = true;
$.each(gallery, function(index, obj){
if (!keepGoing)
return true;
if (obj.img == src){
alert(obj.title);
keepGoing = false;
}
})
});
这是一把小提琴:
http://jsfiddle.net/v4n4LdxL/1/
为了避免在单击某个图像时遍历所有图像,可以将图像src作为库变量中的键。
示例:
$(function(){
var gallery = {
"img/gallery/gameboy2.png" : {
"title" : "GameBoy Color",
"about" : "A-ball",
"price" : 99,
"category" : ["gameboy", "color", "console", "game"]
},
"img/gallery/phone.png": {
"title" : "Hamburger Phone",
"about" : "What is a smartphone?",
"price" : 129,
"category" : ["phone","hamburger"]
}
};
$('ul li').click(function(){
var src = $(this).find('img').attr('src');
var img = gallery[src];
alert(img.title);
});
});
这是这种方法的关键:
http://jsfiddle.net/v4n4LdxL/2/
编辑:更新了fiddle
您可以使用jQuery查找索引。
例如,您可以运行此点击功能来发送警报
$('ul li').click(function(){ alert($(this).index()); });
相关文章:
- 单击图像时打开“跨度”标记
- 单击图像后如何创建下拉菜单?[引导/角度]
- 当使用 Javascript 单击图像 A 时,如何将图像 B 的 src 更改为图像 A 的 src
- 单击图像滑块计时器的事件
- 单击图像更改图像并调用函数
- 如何通过单击图像按钮打开图像
- Div在单击图像时未执行slideTogle()
- 单击图像时如何播放音乐
- 尝试单击图像并打开模式ajax
- 画布:单击图像
- 使用javascript和php,在单击图像时将其移动到另一个位置
- 单击图像时播放声音
- 单击图像时不会出现圆圈,但对其他人有效
- 图像可以'单击图像时不会显示
- 单击图像时从数据库加载选项id
- 单击图像,获取坐标并将其保存在数据库JavaScript和Rails中
- 单击图像不执行任何操作
- 单击图像几秒钟后,打开一个链接
- 在 IE7 和 IE8 中单击图像时触发的 window.blur
- 如何在悬停图像时显示某些信息,并在单击图像时显示某些内容