Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 继续从jQuery数组中获取相同的值_Javascript_Jquery_Arrays_Image_Src - Fatal编程技术网

Javascript 继续从jQuery数组中获取相同的值

Javascript 继续从jQuery数组中获取相同的值,javascript,jquery,arrays,image,src,Javascript,Jquery,Arrays,Image,Src,我有两个问题 我有一个jQuery数组,用于存储图像的URL。几分钟前一切正常,但现在不行了。我有以下图像代码: 变量图像=[link1、link2等]; 指数=0 单击它们的缩略图时,此选项将显示它们: $('.thumbnail').click(function(){ $('.large_view').prepend('<img src="'+images[index]+'" width="450px"/>'); }); 我的问题是,当我单击缩略图时,数组中的第一个图像总是显示

我有两个问题

我有一个jQuery数组,用于存储图像的URL。几分钟前一切正常,但现在不行了。我有以下图像代码:

变量图像=[link1、link2等]; 指数=0

单击它们的缩略图时,此选项将显示它们:

$('.thumbnail').click(function(){
$('.large_view').prepend('<img src="'+images[index]+'" width="450px"/>');
});
我的问题是,当我单击缩略图时,数组中的第一个图像总是显示相同的图像。之后,我可以使用“下一步/上一步”按钮正确更改图像


当我用包含以下内容的代码显示数组中的图像时,它会向后显示,从数组中的最后一项到第一项:forvar I=0;i第一个问题是,当单击任何缩略图时,索引将处于其原始值0,这总是使第一个缩略图显示出来。要解决这个问题,如果您将索引填充到这样的属性中,则需要使用$this.attr“data-image-index”从单击的缩略图中确定相应的索引

第二个问题是,您希望在for循环中使用.append而不是.prepend将每个新缩略图固定到集合的末尾而不是开始处

此外,.next和.previous的单击处理程序似乎是向后的


您可能需要使用$'.large_view.html。。。而不是$'.large_view'.prepend。。。如果您在任何给定时间只希望显示一张大图片。

1您的索引为0,因此每次都将获得第一张图像。 这条线

$'.large_view'.prepend;
$('.next').click(function(){
         index = (index===0)?(images.length-1):(index-1);
          $('.large_view img').attr('src',images[index]);
         });

$('.previous').click(function(){
           index = (index==images.length-1)?0:(index+1);
           $('.large_view img').attr('src',images[index]);
           });