确保图像在 Ajax 调用中预加载

发布于 2024-10-07 21:07:25 字数 649 浏览 0 评论 0原文

我注意到 jQuery Ajax 调用在回调之前不会预加载/加载目标页面的图像。所以基本上它只是加载 html,然后进行回调,回调后它开始加载图像。

所以我的问题是是否有一种方法可以确保目标页面图像也在附加函数之前加载/预加载。

示例代码:

$.ajax({
   url: ''+nextHref+'',
   success: function(data) {

   var $response = $(data);
   var oneval = $response.find('#ContainerWrapper').html();

   // Then some kind of function that preloads the target images before it appends the ajax result. So basically the part below is where I don't know what o do.
   $response.find('#ContainerWrapper img').examplepreloadfunction(function(){

      $("#ContainerWrapper").append(oneval);

   });

   }
});

有任何答案、线索、想法或实验吗?

I have noticed that a jQuery Ajax call don't preload/load the images of the target page before it callbacks. So basically it just loads the html and then make the callback and after the callback it starts to load the image/images.

So my question is if there's a way to make sure that the target page images also gets loaded/preloaded before the append function.

Example Code:

$.ajax({
   url: ''+nextHref+'',
   success: function(data) {

   var $response = $(data);
   var oneval = $response.find('#ContainerWrapper').html();

   // Then some kind of function that preloads the target images before it appends the ajax result. So basically the part below is where I don't know what o do.
   $response.find('#ContainerWrapper img').examplepreloadfunction(function(){

      $("#ContainerWrapper").append(oneval);

   });

   }
});

Any answers, clues, ideas or experiments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2024-10-14 21:07:25

预加载图像:

$('<img/>')[0].src = '/path/to/image';

或者:

(new Image()).src = '/path/to/image';

在将从 AJAX 调用收到的 HTML 注入 DOM 之前,您可以对每个图像尝试此操作。

To preload an image:

$('<img/>')[0].src = '/path/to/image';

or:

(new Image()).src = '/path/to/image';

You could try this for each image before injecting the HTML you received from the AJAX call into the DOM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文