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

标签 javascript jquery dom load

我有一个包含许多图像的 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/

相关文章:

javascript - 删除 Chart.js 图表填充

javascript - 如何在 javascript 中保护 location.href 免受跨站点脚本的影响?

javascript - CodeIgniter - 使用 setInterval 自动从数据库获取数据

javascript - 在同一 DOM 上使用多个 Angular.js Controller

javascript - jQuery 解析带有段落错误的原始 HTML

javascript - 现在可以在没有 .NET 4.5 主机的情况下使用 Web 套接字吗?

javascript - 将组合 JSON 文件加载到 Google map 上

jquery - 在 Jquery 中循环 div 内容 Upwords 喜欢(最新消息)得到错误

javascript - 如何使用一个表格填写多个表格

dom - 更新dom元素 Electron