gpt4 book ai didi

JavaScript : passing a function as an argument

转载 作者:行者123 更新时间:2023-11-28 20:12:46 24 4
gpt4 key购买 nike

如果图像加载失败,我想加载默认图像。我也想打电话图像加载后,使用 getLoadComplete() 方法。如何将 this.getLoadComplete() 函数传递到 img.onerror 上调用的函数中?

这是我得到的代码:

    img.onload = this.getLoadComplete();

img.onerror = function(){
img.src = "http://xxxx.com/image.gif";
}

img.src = request.url;

最佳答案

img.onload = this.getLoadComplete();

您立即调用 getLoadComplete(),并将其返回值分配给 img.onload。您可能想要这个:

img.onload = this.getLoadComplete;

也就是说,将 img.onload 设置为函数本身,而不是其返回值。

您不需要在onerror中执行任何特殊操作; onload 处理程序将仍然设置为 getLoadComplete,并且当您修改 onerror 中的 src 处理程序,它将在加载后备图像后调用 onload

关于JavaScript : passing a function as an argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19643528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com