我试图用来自端点的数据构建轮播。我不知道要在服务器中使用哪种尺寸,所以在前端我会做一些处理来决定要使用的图像的大小。之后,我隐藏未使用的图像。当我做display:none
它仍然会触发HTTP请求,从而伤害我的表现。我试图使用remove()
而不是.css('display','none');
,但因为我正在使用类,所以删除了其他图像。在循环内移除img
如何在循环中删除图像而不影响其他图像?
这个数据,我从服务器(组件)获得以下10个。
<div class="foo-grid-img">
<img src="https://cdn.com/image/1.jpg" class="foo-big" />
<img src="https://cdn.com/image/2.jpg" class="foo-small" />
<img src="https://cdn.com/image/3.jpg" class="foo-horizontal" />
<img src="https://cdn.com/image/4.jpg" class="foo-vertical" />
</div>
<div class="foo-grid-img">
<img src="https://cdn.com/image/a.jpg" class="foo-big" />
<img src="https://cdn.com/image/b.jpg" class="foo-small" />
<img src="https://cdn.com/image/c.jpg" class="foo-horizontal" />
<img src="https://cdn.com/image/d.jpg" class="foo-vertical" />
</div>
var fooConf = [['big'],['vertical','big'],['small','small','horizontal'],['vertical','big','horizontal','horizontal'],['vertical','big','horizontal','small','small']];
for (var i = 0; i < components.length; i++) {
// elided
var fooClass = fooConf[components.length-1][i];
if("foo-"+fooClass != fooBig.attr("class")) {
cItem.find('.foo-big').css('display','none');
}
if("foo-"+fooClass != fooSmall.attr("class")) {
cItem.find('.foo-small').css('display','none');
}
if("foo-"+fooClass != fooHorizontal.attr("class")) {
cItem.find('.foo-horizontal').css('display','none');
}
if("foo-"+fooClass != fooVertical.attr("class")) {
cItem.find('.foo-vertical').css('display','none');
}
}
我需要一个最终结果,如下面呈现为html。
<div class="foo-grid-img">
<img src="https://cdn.com/image/1.jpg" class="foo-big" />
</div>
<div class="foo-grid-img">
<img src="https://cdn.com/image/d.jpg" class="foo-vertical" />
</div>
我不知道如果我理解正确你的问题。请确认我的理解:您从API获取5张图片,并从给定的5张图片中选择一张。正确? –
尝试使用$ .each() – Osama
5套图像集。像上面那些。他们都有定义相同类的图像。看每组有4个图像,我需要从每个组中删除3,每组1。 – DarthVader