Javascript JS在类中隐藏损坏的图像
我知道如何隐藏图像,如果它们如下所示Javascript JS在类中隐藏损坏的图像,javascript,jquery,Javascript,Jquery,我知道如何隐藏图像,如果它们如下所示 <img src=""......> 不平凡 试一试 $(function(){$(“[data bg]”)。每个(function(){const$parent=$(this).最近的(.lx gallery”)const img=new Image();img.onerror=function(){$parent.hide();}img.src=$(this).data(“bg”)})都工作得非常好。谢谢你。你可以只需c
<img src=""......>
不平凡
试一试
$(function(){$(“[data bg]”)。每个(function(){const$parent=$(this).最近的(.lx gallery”)const img=new Image();img.onerror=function(){$parent.hide();}img.src=$(this).data(“bg”)})都工作得非常好。
谢谢你。你可以只需const$this=$(this)
然后$this.hide()
如果要用背景隐藏div
<div class="lx-g3-f" >
<div class="lx-gallery" data-bg="image_url" alt="alt text" referrerpolicy="no-referrer" title="something title">
<div class="lx-gallery-title" >
<h3><a href='something_img_url' referrerpolicy="no-referrer">click here!</a></h3>
</div>
</div>
$("img").on("error", function() {
$(this).closest('.lx-g3-f').hide();
});
$(function() {
$("[data-bg]").each(function() {
const $parent = $(this).closest(".lx-g3-f")
const img = new Image();
img.onerror=function() {
$parent.hide();
}
img.src=$(this).data("bg")
})
})