2014-10-30 39 views
0

我正在制作一种画廊,目的应该是,如果您点击一个图像,并在屏幕上显示一个较大的图像。有了这个图片,它应该是一个标题和一个文本。我的问题是,我怎样才能找出哪一个被点击,以便正确的图像和正确的文字和标题显示?找出使用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")); 


} 
+0

当你添加任何元素到页面中,您可以附加对该元素进行“onClick”回调。 onClick中提到的功能将在单击该项目时被调用。通过onClick传递的事件将识别哪个元素被点击。 – Kolban 2014-10-30 14:32:49

+0

我已经使用“addEventListener”而不是onClick,听说它是一个更好的方法。 – teninchhero 2014-10-30 14:39:34

回答

0

您可以通过使用jQuery找到索引。

例如,您可以运行此点击功能发送警报

$('ul li').click(function(){ alert($(this).index()); }); 
+0

你能解释一下我的代码吗? – teninchhero 2014-10-30 14:51:22

1

试试这个,

$('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/

编辑:更新小提琴

+0

所以我只是在我的数组下添加并不关心函数'showItem'? – teninchhero 2014-10-30 17:17:23

+0

您可以将上述示例中的'alert'替换为事件启动时要调用的任何函数,包括'showItem' :) – foxygen 2014-10-30 18:01:22

+0

非常感谢您的帮助,但jQuery对我来说仍然有点困难..最后在每个img元素上添加一个id并使用'this.id'来获得正确的结果。 – teninchhero 2014-10-30 21:13:02