2015-02-10 35 views
0

我知道如何使用javascript制作图片库..当我点击一张图片时,它会变得更大......好吧,如果我有50张图片...我必须为我写50个函数画廊工作! ... 我的问题是...有没有一种方法来写一个简短的功能,这将节省我的时间和负担?如何让图片库更容易

例如,这是功能之一:

function changeImg1(){ 
    $("#show").hide("explode"); 
    $("#show").show("bounce"); 
    $("#captionText").hide("explode"); 
    $("#captionText").show("pulsate"); 
    showImage.setAttribute("src",images[1]); 
    showCaption.innerHTML=(captions[1]); 
    imageIndex=1; 
    captionIndex=1; 
}; 

非常感谢球员,我从您那里学到了很多......

回答

0

你需要阅读有关“本”的JS和jQuery 。例如

$(".many_many_img_with_same_class").click(function(){ 

    var current_img_src = $(this).attr("src"); 

    alert(current_img_src); 

}); 

所以比你点击img,函数运行在这个img的上下文中,并且总是显示被点击的img的src。 p.s.在评论

HTML

<img src="/some/place1" data-any-spec-text="my text 1" class="many_many_img_with_same_class"> 
<img src="/some/place124" data-any-spec-text="my text 222" class="many_many_img_with_same_class"> 
<img src="/some/other/place" data-any-spec-text="more text 222" class="many_many_img_with_same_class"> 

JS

$(".many_many_img_with_same_class").click(function(){ 

    var current_img_src = $(this).attr("src"); 
    var current_img_capton_info = $(this).data("any-spec-text"); 

    alert(current_img_src, current_img_capton_info); 

}); 
+0

确定anower问题,我会尝试这一个,并尽快给您 – 2015-02-10 08:43:53

+0

@deathdragon不要”忘记添加的jQuery – kpblc 2015-02-10 08:46:29

+0

是它的工作.. 。谢谢很多人......但我还有一件事...如果我想在每张图片下添加一个标题......并且标题随图像自动更改...我是否使用相同的功能或者是什么 ? ...哦,我有一个数组中的字幕在JavaScript本身的var里面...我希望我的问题已经足够清楚了...... – 2015-02-10 09:22:35