jQuery iframe load() 事件? [英] jQuery iframe load() event?

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

问题描述

有人知道有没有这种东西?

Does anyone know if there is such a thing?

我有一个使用 $.ajax() 插入的 iframe,我想在 iframe 中的内容完全加载后做一些事情:

I have a iframe that's being inserted with $.ajax() and I want to do some stuff after the contents from the iframe are completely loaded:

....
 success: function(html){  // <-- html is the IFRAME (#theiframe)
          $(this).html(html);   // $(this) is the container element
          $(this).show();
          $('#theiframe').load(function(){
             alert('loaded!');
          } 
....

它有效,但我看到 IFRAME 加载了两次(警报也显示了两次).

it works, but I see the IFRAME is loaded twice (the alert also shows twice).

推荐答案

如果可能,您最好处理 iframe 文档中的 load 事件并调用包含中的函数文档.这样做的好处是可以在所有浏览器中运行并且只运行一次.

If possible, you'd be better off handling the load event within the iframe's document and calling out to a function in the containing document. This has the advantage of working in all browsers and only running once.

在主文档中:

function iframeLoaded() {
    alert("Iframe loaded!");
}

在 iframe 文档中:

In the iframe document:

window.onload = function() {
    parent.iframeLoaded();
}

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

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