Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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
Jquery 将每个图像包装在一个div中,并在前面加上一个数字_Jquery_Html_Image - Fatal编程技术网

Jquery 将每个图像包装在一个div中,并在前面加上一个数字

Jquery 将每个图像包装在一个div中,并在前面加上一个数字,jquery,html,image,Jquery,Html,Image,我试图编写一个函数,将每个图像包装在一个div中,然后为每个图像插入一个带有标签的span 这是我到目前为止写的: $('#carousel img').each(function(index) { $(this).wrap('<div class="image"></div>'); $(this).before(function (index) { index = '00' + (index + 1); return '<span clas

我试图编写一个函数,将每个图像包装在一个div中,然后为每个图像插入一个带有标签的span

这是我到目前为止写的:

$('#carousel img').each(function(index) {
  $(this).wrap('<div class="image"></div>');
  $(this).before(function (index) {
    index = '00' + (index + 1);
    return '<span class=index>' + index.substr(index.length - 2) + '</span>';
  });
});
$('#carousel img')。每个(函数(索引){
$(此).wrap(“”);
$(此).before(函数(索引){
索引='00'+(索引+1);
返回“”+index.substr(index.length-2)+“”;
});
});
我的代码实际上是这样做的,请注意数字问题,并将跨度放在img标记内

<div class="image">
  <img src="img/ny1.jpg" />
    <span class="index">01</span>   
  </img>
</div>

<div class="image">
  <img src="img/ny2.jpg" />
    <span class="index">01</span>   
  </img>
</div>

01
01
此操作的预期结果:

由此:

为此:

<div class="image">
<span class="index">01</span>   
<img src="img/ny1.jpg" />
</div>

<div class="image">
<span class="index">02</span>   
<img src="img/ny1.jpg" />
</div>

01
02

谢谢

如果您遇到的问题是编号问题(因为我看不出其他错误),请从
$(此)。在(函数(索引){


您遇到的问题是…?问题是它将跨度放入img包装中