我有这样的事情:
<div class="l-logo-window">
<img alt="X" src=".../7e1571ec597e_18dbe64431">
<img alt="X" src=".../073c03a63161_a7b6976ea8">
<img alt="X" src=".../a3d64882c398_0a8a1e5c9a">
...
<img alt="X" src=".../ae6082c9b3b1_fc020700ee">
<img alt="X" src=".../d154cb017e7a_f03972285a">
<img alt="X" src=".../975809963413_6c999604ab">
我试图将它们拆分为 div,并在该 div 中制作另外 2 个 div,每个 4 个 imgs,以获得如下结果:
<div class="l-logo-window">
<!-- 8 Elements in total - wrap -->
<div>
<!-- 4 Elements in div -->
<div>
<img alt="X" src=".../7e1571ec597e_18dbe64431">
<img alt="X" src=".../073c03a63161_a7b6976ea8">
<img alt="X" src=".../a3d64882c398_0a8a1e5c9a">
<img alt="X" src=".../3318b65041f2_3447b0bf60">
</div>
<!-- 4 Elements in div -->
<div>
<img alt="X" src=".../7e1571ec597e_18dbe64431">
<img alt="X" src=".../073c03a63161_a7b6976ea8">
<img alt="X" src=".../a3d64882c398_0a8a1e5c9a">
<img alt="X" src=".../3318b65041f2_3447b0bf60">
</div>
</div>
<!-- 8 Elements in total - wrap -->
<div>
<!-- 4 Elements in div -->
<div>
<img alt="X" src=".../7e1571ec597e_18dbe64431">
<img alt="X" src=".../073c03a63161_a7b6976ea8">
<img alt="X" src=".../a3d64882c398_0a8a1e5c9a">
<img alt="X" src=".../3318b65041f2_3447b0bf60">
</div>
<!-- 4 Elements in div -->
<div>
<img alt="X" src=".../7e1571ec597e_18dbe64431">
<img alt="X" src=".../073c03a63161_a7b6976ea8">
<img alt="X" src=".../a3d64882c398_0a8a1e5c9a">
<img alt="X" src=".../3318b65041f2_3447b0bf60">
</div>
</div>
</div>
我懂了:
$('.l-logo-window > img:nth-child(8n-7)').each(function() {
$(this).nextAll().slice(0, 7).add(this).wrapAll('<div class="subclass-js"></div>');
}).eq(0).closest('div').unwrap();
$('.subclass-js > img:nth-child(4n-3)').each(function() {
$(this).nextAll().slice(0, 3).add(this).wrapAll('<div></div>');
}).eq(0).closest('div').unwrap();
但它有 2 个缺点: - 第一:我丢失了我的 wrap div[class="l-logo-window"], - 第二:前两个 div 缺少一个 div。
请帮帮我,谢谢!