gpt4 book ai didi

javascript - 重新加载后,iframe 中的 OnLoad 事件不会在 IE8 中触发

转载 作者:行者123 更新时间:2023-11-28 10:05:08 25 4
gpt4 key购买 nike

我的页面上有一个 iframe,我正在尝试重新加载内容。在所有其他浏览器中,只需执行以下操作即可实现:

$('#myFrame').contentWindow.location.reload(true)

但是,这在 IE 中不起作用(在 9 和 8 中进行了测试),我发现可以通过克隆 iframe 容器、将克隆添加到页面并删除原始 iframe 来重新加载它,如下所示:

var pe = $(window.parent.frames['myFrame'].frameElement.parentElement);
clonedPe = pe.clone();
clonedPe.insertBefore(pe);
pe.remove();

我遇到的问题是,在原始 iframe 上,我在 onload 事件中添加了一些代码,但这不会在加载克隆 iframe 时触发。

我的问题是......是否有更好的方法在 IE 中重新加载 iframe - 或者 - 如何让 onload 事件在重新加载时触发!?!

最佳答案

如果您更改 iframe 的源,它将重新加载:

var pe = $(window.parent.frames['myFrame'].frameElement.parentElement);
$.data(pe, 'src', pe.attr('src'));
pe.attr('src', 'about:blank').attr('src', $.data(pe, 'src'));

这是一个演示:http://jsfiddle.net/4ZgKh/1/

您还可以使用 .delegate() 将事件绑定(bind)到 iframe,以便事件处理程序对于绑定(bind)事件处理程序后插入到 DOM 中的任何匹配元素都保持 true:

$(document).delegate('iframe', 'load', function () {...});

请注意,在 jQuery 1.7 及更高版本中,首选方法是:

$(document).on('load', 'iframe', function () {...});

关于javascript - 重新加载后,iframe 中的 OnLoad 事件不会在 IE8 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8508777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com