1、背景
在页面中放了一个display:none的iframe,当用户点击一个button时,用该iframe去拉取一个网页,并将iframe显示出来。
2、问题
给iframe添加了onload事件,当iframe显示出来后,不关闭,并再次点击button,在IE下onload事件不触发。
3、问题分析与解决办法
IE 支持 iframe 的 onload 事件,不过对动态创建和修改的onload事件函数没有反应(不会被触发),如:
iframe.onload = function(){alert(1)};
而需要通过 attachEvent 来注册:
FaddEvent('iframeid', 'load', function(){alert(1)}, true);
function addEvent(e,evt,fn,isID)
{
if(isID==true) e=Fid(e);
if(e.attachEvent &&
(typeof(e.attachEvent)=="function" || typeof(e.attachEvent)=="object"))
{
e.attachEvent("on"+evt,fn);
}
else if(e.addEventListener &&
(typeof(e.addEventListener)=="function" || typeof(e.addEventListener)=="object"))
{
e.addEventListener(evt,fn,false);
}
}