【问题标题】:Javascript/jQuery HasLoaded or equivalent?Javascript/jQuery HasLoaded 还是等效的?
【发布时间】:2010-05-17 14:47:23
【问题描述】:

我知道在 jquery 中可以调用 javascript/jquery onload()/load() 函数,例如图像 (<img>)。

但是,如果在 jquery 中,如果我在 dom 加载后使用 .html(htmlString) 插入图像,我如何添加侦听器来处理图像 onload 事件?是否有一个属性我可以检查以查看各种图像以及它们是否已加载?

【问题讨论】:

    标签: javascript jquery fsevents


    【解决方案1】:

    添加 html 后,您可以将加载事件绑定到包含的图像:

    $("#foo").html(htmlString).find("img").one("load", function() {
        ...
    }).each(function() {
    
        // image has been cached, so load event won't fire unless we explicitly call it
        if(this.complete) $(this).trigger("load");
    });
    

    【讨论】:

    • 创建图像然后直接绑定它而不是进行这种冗余选择不是更好吗?
    • 确保在这种情况下使用.one('load', 而不是.load(。 @J-P:是的,但是你不会回答这个问题..
    • @J-P - 我从这个问题中了解到,他想将load 绑定到属于任意 HTML 字符串的图像。 @Nick - 干杯,在答案中更新了。
    • @Nick,这是真的。但似乎我们至少应该提到做这种事情的理想方式......而不是被动地回答直接问题。
    • @karim79,如果是这样,那好吧。
    【解决方案2】:

    检查图像的complete 属性

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 2020-10-28
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 2010-11-19
      相关资源
      最近更新 更多