replaceWith后jQuery的负载事件 [英] jQuery load-event after replaceWith

查看:99
本文介绍了replaceWith后jQuery的负载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图阿贾克斯加载一些内容,然后换上新的下载内容在页面上现有的内容。问题是,我需要绑定负载(处理器(的EventObject))事件替换数据。我需要一个触发时加载的所有图像。这是我到目前为止有:

i'm trying to ajax load some content and then replace existing content on the page with the newly downloaded content. The problem is that I need to bind load(handler(eventObject)) event for replaced data. I need that to trigger when all images are loaded. Here is what I have so far:

$("#mainContentHolder").live("load", function(){ 
   alert("images loaded!") 
});

$.get("content.htm", function(data){
    $("#mainContentHolder").replaceWith(data);
    alert("content is loaded!");
});

我看到的内容加载时发出警报,但它发生之前的图像加载和警报的图像加载从未发生过(我也试过绑定()而不是生活()之前)。 有谁知道一个补丁是什么?

I see an alert when the content is loaded, but it happens before images are loaded and alert on images load never happens (I also tried bind() instead of live() before). Does anyone know a fix for that?

推荐答案

这可能是也可能不是你的问题,但它看起来像您所附加的图像加载功能,当你加载的AJAX内容被替换的容器:

This may or may not be your problem, but it looks like the container you have attached your image load function to is being replaced when you load the ajax content:

$("#mainContentHolder").live("load", function(){ //you are attaching to current and future '#mainContentHolder' elements
   alert("images loaded!") 
});

$.get("content.htm", function(data){
    $("#mainContentHolder").replaceWith(data);  //'#mainContentHolder' element is replaced with something else
    alert("content is loaded!");
});

不知道哪些内容是从您的AJAX调用回来了,但如果没有一个 #mainContentHolder 元素中,将有没有为你的图像加载事件处理程序附加到。

Not sure what content is coming back from your AJAX call, but if it doesn't have a #mainContentHolder element, there will be nothing for your image load event handler to attach to.

如果这不是它,但也就是这一点:(从 http://api.jquery.com /负载事件/

If that's not it, there's also this bit: (from http://api.jquery.com/load-event/)

有可能的是,如果该图像被从浏览器高速缓存加载的加载事件将不被触发。考虑到这种可能性,我们可以使用立即触发如果图像是准备一个特殊的加载事件。 event.special.load目前可作为插件

It is possible that the load event will not be triggered if the image is loaded from the browser cache. To account for this possibility, we can use a special load event that fires immediately if the image is ready. event.special.load is currently available as a plugin.

希望那些人会帮助你。

这篇关于replaceWith后jQuery的负载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆