jQuery .load()不能处理我的图像

Eti*_*uis 38 jquery

我有一些代码,我试图在我的图像加载完成后运行.我使用以下jQuery代码:

$("#myimageid").load(function() {
    alert('Image Loaded');
});
Run Code Online (Sandbox Code Playgroud)

然而,弹出窗口永远不会出现.我无法让.load()函数工作!有人有这个问题吗?

Nic*_*ver 105

如果您在图像已经设置了源之后运行它,则需要对缓存图像(在您添加监听事件的事件处理程序之前触发事件的人)进行额外检查.你可以这样做:

$("#myimageid").on('load', function() {
  alert('Image Loaded'); 
}).each(function() {
  if(this.complete) $(this).load();
});
Run Code Online (Sandbox Code Playgroud)

更新更高版本的查询,使用:

if(this.complete) $(this).trigger('load');
Run Code Online (Sandbox Code Playgroud)

使用(this).load();会产生Cannot read property 'indexOf' of undefined错误

  • 在jquery 3`if(this.complete)$(this).trigger('load');` (4认同)
  • 很好,为什么这么难找! (2认同)