【问题标题】:jquery callback on new image loaded via ajax call [duplicate]通过ajax调用加载的新图像上的jquery回调[重复]
【发布时间】:2014-09-14 11:53:36
【问题描述】:

我有一个函数来获取图像的宽度和高度,但它没有响应通过 ajax 调用加载的新图像。

函数是 -

$('img').on('load',function() {
    console.log(this.width + 'x' + this.height);
});

但我正在使用 ajax 调用将 DOM 添加到页面,但上面的代码没有响应正在加载的新图像

我的示例页面是here in the link,带有控制台日志。

问候

【问题讨论】:

  • 添加DOM时必须附加onLoad事件
  • 我已经编辑了问题并添加了示例页面
  • 我认为@Andy 你应该删除标签“问题可能在这里有答案”
  • @PradyutBhattacharya,我做不到。

标签: javascript jquery onload-event


【解决方案1】:

按照Rafael Diaz说的去做

$.ajax({
        type: "POST",
        url: 'bAlbum.jsp',
        data: null ,
        cache: false,
        success: function(html){
            $('ol#imagelist').append(html);               

            $("ol#imagelist li").each( function (i, e)
            {

               $(e).find('img').one('load', function() {
                   console.log(this.width + 'x' + this.height);
               });
            });
        }
    });

【讨论】:

    【解决方案2】:

    你需要这样做

    $("img").one("load", function() {
    
    }).each(function() {
      if(this.complete){
       console.log(this.width + 'x' + this.height);
     }
    });
    

    更新

    $("div.albumList").on("load","img", function() {
          console.log(this.width + 'x' + this.height);
    
      });
    

    【讨论】:

    • 不,这没有帮助,我得到 0x0
    • 你也可以添加你的html部分吗?
    • 我已经用示例页面编辑了我的问题。
    • 查看我的更新答案
    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 2011-09-20
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多