Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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 使用JS中的共享类按顺序动态填充图像_Javascript_Jquery - Fatal编程技术网

Javascript 使用JS中的共享类按顺序动态填充图像

Javascript 使用JS中的共享类按顺序动态填充图像,javascript,jquery,Javascript,Jquery,我有下面这样的东西,标记明智。我有一个图像文件夹,里面有“thumb1、thumb2、thumb3”等中的所有拇指图像(但大约100个)。我想从“thumb1”开始填充,作为填充thumb类第一个实例的第一个实例,然后向下填充,2、3、4等等,但不涉及标记。这可以用JS来完成吗?可以检测订单吗;第一个实例=1 <div class="thumb"></div> <div class="thumb"></div> <div class="thu

我有下面这样的东西,标记明智。我有一个图像文件夹,里面有“thumb1、thumb2、thumb3”等中的所有拇指图像(但大约100个)。我想从“thumb1”开始填充,作为填充thumb类第一个实例的第一个实例,然后向下填充,2、3、4等等,但不涉及标记。这可以用JS来完成吗?可以检测订单吗;第一个实例=1

<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>

假设缩略图的路径是静态的,并且已知您可以这样做:

$('.thumb').each(function(index){
    var src = 'path/to/img/thumb' + index+1;
    var $img = $('<img />').attr('src', src);
    $(this).html($img);
});
$('.thumb')。每个(函数(索引){
var src='path/to/img/thumb'+索引+1;
变量$img=$('
  • 使用类缩略图迭代每个
    div
  • 使用元素的
    索引
    引用图像(
    +1
    ,因为索引从0开始)
  • 将新的
    img
    -元素添加到设置了路径的div中
您也可以使用或将
img
添加到
div
,而不是


参考


您想将拇指图像放在div中吗?拇指的数量是否会改变?