Jquery 与.on有关的问题(';错误';)

Jquery 与.on有关的问题(';错误';),jquery,Jquery,我想从中获取“数据默认值”。如果其“src”返回错误,请检查img,然后将该值分配给其同级源的src 其思想是检查移动版本图像是否可用,如果不可用,则获取默认值,并通过将其指定为srcset以在移动源中显示默认图像 这似乎不起作用,对不存在的图像的请求正在反复发生 <picture> <!-- Mobile image --> <source srcset="data-defaultimg value should be here if .chec

我想从中获取“数据默认值”。如果其“src”返回错误,请检查img,然后将该值分配给其同级源的src

其思想是检查移动版本图像是否可用,如果不可用,则获取默认值,并通过将其指定为srcset以在移动源中显示默认图像

这似乎不起作用,对不存在的图像的请求正在反复发生

<picture>

    <!-- Mobile image -->
    <source srcset="data-defaultimg value should be here if .check-img (mobile image src) returns error" media="(max-width:768px)">

    <!-- Virtual image with desktop image as data-defaultImage value --> 
    <img style="display: none" src="mobile image src here" alt="" class="check-img" data-defaultimg="default src here">

    <!-- Desktop image. -->
    <img src="desktop-img-src" alt="" class="img-fluid">

</picture>

我不知道为什么丢失图像的请求一直在进行。请帮忙。谢谢。

为避免重复加载未找到的图像,只需在处理错误后删除错误事件即可

$(“图片img.check img”)。每个(函数(){
$(this).on('error',function(){
$(this.off('error');
$(this.this(“source”).attr(“srcset”),$(this.data('defaultimg'));
})
});


A显示:无图像甚至可能无法下载是的,只需删除
display:none,图像甚至没有加载,这就是它永远不会失败的原因。尝试删除“显示:无”,但它仍然会执行相同的操作。好的,让我检查一下我是否复制了它,它仍然可以工作。你的错误是什么?
$("picture img.check-img").each(function(){
  $(this).on('error', function(){
    $(this).siblings("source").attr("srcset", $(this).data('defaultimg'));
  })
});