我正在编写一个firefox插件,该插件可以在网页中打开iframe。我想在iframe中选择对象,但无法访问内容:
var iframe = content.document.getElementById("MyBeautifulIframe");
if (iframe)
{
var mydiv = iframe.contentDocument.getElementById("mydiv");
}
Erreur:TypeError:iframe.contentDocument未定义
我尝试了iframe.content.document.getElemen ...,iframe.document.getElemen ...相同的结果。
如何访问iframe dom?如果我查看iframe var类型,则为[object XrayWrapper [object XULElement]],如何访问XULElement对象的dom对象?
最佳答案
更新的答案:
看了一下您的代码,我想我可能已经找到了您的问题。
您正在执行:
var iframe = content.document.getElementById("muzich_iframe_addcontainer");
if (iframe)
{
if (iframe.contentDocument.getElementById("div"))
{
this.close_all();
}
}
muzich_iframe_addcontainer
是div,而不是iframe,因此它永远不会包含contentDocument。另外,我无法通过创建xul元素使其工作。我必须创建html div和iframe才能使其正常工作。
这是代码:
var htmlns = "http://www.w3.org/1999/xhtml";
var container = document.createElementNS(htmlns,'div');
container.setAttribute("id", "muzich_iframe_addcontainer");
container.setAttribute("style", styleContainer); //styleContainer is the one you use without the background stuff
window.content.document.body.appendChild(container);
var iframe = document.createElementNS(htmlns,'iframe');
iframe.setAttribute("id", "muzich_iframe");
iframe.setAttribute("style",
"width: 100%; height: 100%; "
);
iframe.setAttribute("src", "http://www.lifehacker.com"); // Used it as my example url
window.content.document.getElementById("muzich_iframe_addcontainer").appendChild(iframe);
然后,当您要检查关闭时,请执行以下操作:
var iframe = window.content.document.getElementById("muzich_iframe");
if (iframe)
{
if (iframe.contentDocument.getElementById("div"))
{
this.close_all();
}
}
希望这个可以为您解决。