gpt4 book ai didi

javascript - 在执行之前检查是否加载了子元素(图像)?

转载 作者:行者123 更新时间:2023-11-30 18:30:12 25 4
gpt4 key购买 nike

我有一个包含许多图像的 div。我想在执行某些代码之前检查是否已加载此 div 中的所有图像。我无法处理整个文档,因为页面上还有其他内容可能需要更长时间才能加载。

我该怎么做? jquery 可供使用。

最佳答案

你应该起诉 load event

$('img').load(function(){
//here do what you need to do when the img is loaded
});

注意事项

Caveats of the load event when used with images

A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:

  • It doesn't work consistently nor reliably cross-browser
  • It doesn't fire correctly in WebKit if the image src is set to the same src as before
  • It doesn't correctly bubble up the DOM tree
  • Can cease to fire for images that already live in the browser's cache

关于javascript - 在执行之前检查是否加载了子元素(图像)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9870858/

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