【问题标题】:jQuery Event when New Page Loads in iFrame在 iFrame 中加载新页面时的 jQuery 事件
【发布时间】:2010-12-15 17:10:23
【问题描述】:

我正在尝试在父级上设置一个事件,该事件在 iframe 加载新页面时触发(可能通过用户单击链接)。

HTML:

<iframe name="posts" id="posts" src="edit.php">

JS:

$(document).ready(function() {
    $('iframe#posts').live('load', function() {
        console.log('live');
    });
});

问题是console.log() 语句永远不会被触发。我怎样才能实现我想要做的事情?

【问题讨论】:

    标签: jquery events iframe


    【解决方案1】:

    解决问题的最简单方法是使用 jQuery 创建 iframe。

    $(document).ready(function() {
        $("<iframe />").attr({
            id: "posts",
            src: "edit.php"
        }).load(function() {
            console.log('live');
        }).appendTo("body");
    

    这个问题是asked here before

    【讨论】:

      【解决方案2】:

      我的使用方式是

      <iframe onload="hide_html_player_contents_loading();" ...../>
      

      【讨论】:

      【解决方案3】:

      我最好的猜测是 iframe 上的 'load' 事件在文档上的 'ready' 事件之前触发,这意味着处理程序安装得太晚了。您可能必须在 iframe 元素本身中使用老式的 onload

      或者您可以将$('iframe#posts').live() sn-p 放在顶级脚本中,而不是放在就绪处理程序中。

      【讨论】:

      • 老派&lt;iframe onload="..."&gt; 工作得很好。我尝试将.live() 调用放在全局范围内,但它似乎仍然没有做任何事情。
      • @dosboy 是有道理的,如果 Dan 的猜测是正确的。 iframe 仍会在文档之前触发。
      猜你喜欢
      • 2011-04-18
      • 1970-01-01
      • 2011-08-12
      • 2014-09-23
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多