jquery - 隐藏包含损坏图像的 div

标签 jquery html css

所以我正在开发一个博客/网站,其中有一个图像轮播,其中有 8 个图像点。我正在将 Owl Carousel 与 CouchCMS 一起使用(这应该不会太重要)..

这是轮播的样子:

<div id="owl-demo" class="owl-carousel">
     <div class="item"><img src="<cms:show blog_image />" class="carousel_image"></div>
     <div class="item"><img src="<cms:show image_2 />"></div>
     <div class="item"><img src="<cms:show image_3 />"></div>
     <div class="item"><img src="<cms:show image_4 />"></div>
     <div class="item"><img src="<cms:show image_5 />"></div>
     <div class="item"><img src="<cms:show image_6 />"></div>
     <div class="item"><img src="<cms:show image_7 />"></div>
     <div class="item"><img src="<cms:show image_8 />"></div>
</div>

现在我有了它,您可以使用 CouchCMS 上传最多 8 张图像。

如果您上传的图片少于 8 张,其余的将仅显示为损坏的图片。

现在我正在使用以下代码来隐藏损坏的图像,这当然会留下一个空白的 div(但这仍然比显示损坏的图像更好)...这是我能想到的最好的解决方案与。

$("img").error(function(){
   $(this).hide();
});

有谁知道我可以检测图像是否损坏并隐藏包含该图像的整个 div 的方法吗?

谢谢。

下面是我尝试过的示例,但没有成功。

html

<div class="item" id="img-8-div"><img src="<cms:show image_8 />"></div>

这是 JQuery,它再次不起作用。

$("#img-8-div").children('img').error(function(){
   $(this).hide();
});

最佳答案

Demo Fiddle

使用 jQuery parent()

$("img").error(function(){
   $(this).parent().hide();
});

关于jquery - 隐藏包含损坏图像的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24231749/

相关文章:

Jquery 事件滴流

html - CSS 中心图像水平内联动态宽度父级

html - 我知道 padding 和 margin 之间的区别,但你应该什么时候使用它们?

html - 缩小 html 元素以匹配屏幕尺寸?

javascript - 如何仅在 jquery ui 中的可拖动元素中放置可放置元素

javascript - 运行 jquery 内部的函数 .on ('change' )

javascript - document.getElementById 在 Firefox 中返回元素,但在 Chrome 中不返回元素

javascript - 反向平滑 CSS 动画

javascript - 当我在 ion-content 中使用 iframe 时,ion-content 滚动不起作用

css - 如何将我的导航栏固定到页面顶部