我有一个divid ring-preview,它有一个未指定数量的img元素与其中的类stone-preview.
我想迭代这些子图像并调用:
$(this).rotate(ring.stones[i].stone_rotation);
Run Code Online (Sandbox Code Playgroud)
where this指的是img元素并i指其在的位置div.
我怎样才能做到这一点?
SLa*_*aks 58
你正在寻找这种.each()方法.
例如:
$('.ring-preview').children('img').each(function(i) {
$(this).rotate(ring.stones[i].stone_rotation);
});
Run Code Online (Sandbox Code Playgroud)
如果<img>元素不是直接子元素,则需要调用.find而不是.children.
你可以.each()在这些情况下使用a ,如下所示:
$("#ring-preview img.stone-preview").each(function(i) {
$(this).rotate(ring.stones[i].stone_rotation);
});
Run Code Online (Sandbox Code Playgroud)
回调函数的第一个参数是您所追求的索引.
$('#ring-preview img.stone-preview').each(function(idx, itm) {
$(itm).rotate(stones[idx].stone_rotation);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64360 次 |
| 最近记录: |