【问题标题】:How do I call a function in jquery after a page has loaded within an iframe?在 iframe 中加载页面后,如何在 jquery 中调用函数?
【发布时间】:2013-05-26 20:22:12
【问题描述】:

基本上,我希望 iframe 淡出,然后加载新的 url,然后淡入。现在我的代码只是淡出并且(大概我看不到它)加载新页面。我正在使用$('iframe').ready(function(){ /*code that changes the src of the iframe*/ });

【问题讨论】:

  • 我想我不明白如何从 iframe 内部再次调用 $(iframe).animate() 。这两个似乎都使用 onload 函数,但是我如何从 iframe 内部的页面访问 iframe 的不透明度?也许我只是让自己感到困惑。
  • 您无需在 iframe 中执行任何操作。您将把所有代码放在主页上。

标签: jquery iframe document-ready


【解决方案1】:

这样的?

示例:http://jsfiddle.net/EcRBv/1/

function changeIframe(newSrc){
    $("#iframe1").fadeOut('fast',function(){
        $(this).attr("src",newSrc);
        $(this).load(function(){
            $(this).fadeIn("fast"); 
        });
    });
}

changeIframe("http://www.jsfiddle.net");

【讨论】:

  • 在更改src 之前 附加处理程序可能更明智,以防事件在下一行执行之前设法触发。
  • 是的!!!非常感谢!我只需要将 .ready() 更改为 .load(),我不知道我是怎么错过的。
【解决方案2】:

要么使用.load(),要么更改iframe .attr('src'),像这样。

$(document).ready(function(){
  $('iframe').animate({opacity:0}, 300, function(){
    //load or change source inside this callback     
  })
  $('iframe').animate({opacity:1}, 300};
})

【讨论】:

  • 这不会等待 iframe 页面加载。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 2011-04-18
  • 2016-09-03
相关资源
最近更新 更多