我在div里面有一堆imgs,上面有一个类,在我的JS中我有一个for循环,可以将图像重新调整为一个框内的fir,而不需要拉伸
$('.gallery img')[i].attr('id', 'img' + i);
Run Code Online (Sandbox Code Playgroud)
这就是我试图让每个img拥有自己的id,如'img1''img2''img3'等
但似乎行不通
将for循环替换为:
$('.gallery img').each(function(i) {
$(this).attr('id', 'img' + i);
// You can also add more code here if you wish to manipulate each IMG element further
});
Run Code Online (Sandbox Code Playgroud)
这个代码块循环遍历每个IMG元素,计数器i在每个实例之后递增,并且该计数器作为id字符串的后缀.
| 归档时间: |
|
| 查看次数: |
29807 次 |
| 最近记录: |