我已经花了很长时间尝试不同的方法让这个工作,但是什么都不起作用,文档也没有多大帮助。
我正在尝试填充一个在 iframe 中的表单,而这个 iframe 是我动态注入到页面中的。我注入 iframe 的代码如下:
myObject.iframe = document.createElement("iframe");
myObject.iframe.setAttribute("src", data.url);
myObject.iframe.setAttribute("id","extension-iframe");
myObject.window.document.getElementById('publisher').appendChild(myObject.iframe);
myObject.iframe.addEventListener("load", function(){
myObject.populate(data);
}, false);
这段代码运行良好,并且确实触发了populate()方法。我的问题在于如何获取iframe的document或window对象。我尝试了以下所有方法:
myObject.iframe.window
myObject.iframe.document
myObject.iframe.content
但是这些都没有定义。
我还尝试将事件对象传递给该iframe的加载事件监听器,然后执行:
event.originalTarget
但那也没起作用。
我正在阅读以下文档:
- https://developer.mozilla.org/En/Working_with_windows_in_chrome_code
- https://developer.mozilla.org/en/Code_snippets/Interaction_between_privileged_and_non-privileged_pages
但我要么没有理解它,要么它没有被恰当地解释。
有人能够解释一下吗?
谢谢!