Javascript Can';使用handlebar.js时无法检测IMG jquery错误事件。如何修复这些损坏的图像?

Javascript Can';使用handlebar.js时无法检测IMG jquery错误事件。如何修复这些损坏的图像?,javascript,jquery,html,handlebars.js,Javascript,Jquery,Html,Handlebars.js,我无法检测到源中断的图像的错误事件 图像是在把手中创建的,如下所示: <img src="{{image}}" alt="the image"> 我正在尝试以下代码: console.log('first log'); $('img').one('error', function() { console.log('image error detected'); this.src = "<my other image path here>"; });

我无法检测到源中断的图像的错误事件

图像是在把手中创建的,如下所示:

<img src="{{image}}" alt="the image">

我正在尝试以下代码:

console.log('first log');
$('img').one('error', function() {
    console.log('image error detected');
    this.src = "<my other image path here>";
 });
console.log(“第一个日志”);
$('img').one('error',function(){
console.log(“检测到图像错误”);
this.src=“”;
});
我无法获取我的console.log,除非我在chrome浏览器导航栏中依次单击“后退”和“前进”。如果我先点击“back”再点击“forward”,那么“this.src”的替换也会起作用

我尝试过很多方法,比如加载或将代码放入文档中,等等

当我查看控制台时,我会看到此页面上两个图像的两个GET,然后是上面的“第一个日志”。因此,在绑定我的侦听器之前,图像就已经命中了错误事件。或者类似的东西,也许


有什么建议吗?

终于有了效果。我相信有更好的方法,但这就是我的原因:

  if ($('[data-path="/blahblah"]').length) {
    $('.myClass img').each(function() {
      var image = $(this);
      if (image.context.naturalWidth == 0 || image.readyState == 'uninitialized') {
        image.unbind('error').attr("src", "<placeholder src here>");
      }
    })
  }
if($('[data path=“/blahblah”]').length){
$('.myClass img')。每个(函数(){
var image=$(这是);
if(image.context.naturalWidth==0 | | image.readyState=='uninitialized'){
image.unbind('error').attr(“src”,”);
}
})
}

您是否在错误处理程序触发后绑定事件?我相信是这样。我如何防止这种情况发生?而且,我刚刚意识到,如果我等5分钟,它就会起作用。这和缓存有关,但我不确定是什么。