我有一个加载到iframe中的文档。现在我想知道该文档的“body”元素何时获得事件“焦点”。我已经在其他浏览器中使用addEventListener成功,但IE(8到具体)给了我这个错误:frame.addEventListener在IE上不起作用
Object doesn't support this property or method
我实现这样的:
//This works in Chrome, Opera, Safari and Firefox but not in IE
var myFrame = document.getElementById("my-i-frame");
var frame = (myFrame.contentWindow || myFrame.contentDocument);
frame.addEventListener("focus", function(){alert('works')},false);
我也试过:
frame.document.addEventListener("focus", function(){alert('works')},false);
之前你问为什么要这么做?让我来解释一下:
我创建了一个HTML所见即所得edito r和我想调整我的iframe,当用户点击帧内开始编辑。要做到这一点,我能想到的唯一方法就是进行“关注”事件,然后做我想要做的事情。我希望这听起来清晰(否则原谅我,我不是很会讲英语的:-))。
谢谢Sudhir。我曾尝试使用attachEvent,但我忘记将事件参数“focus”更改为“onfocus”。奇迹般有效 :-) – Sthe 2011-12-29 06:13:09