访问iframe内容(在firefox插件javascript中)

访问iframe内容(在firefox插件javascript中),javascript,iframe,firefox-addon,xul,Javascript,Iframe,Firefox Addon,Xul,我正在写一个firefox插件,在网页中打开一个iframe。我想在iframe中选择对象,但无法访问内容: var iframe = content.document.getElementById("MyBeautifulIframe"); if (iframe) { var mydiv = iframe.contentDocument.getElementById("mydiv"); } Erreur:TypeError:iframe.contentDocument未定义 我尝试使用i

我正在写一个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。。。同样的结果

如何访问iframedom?如果我查看iframe变量类型,它是[object XrayWrapper[object xulement]],如何访问xulement对象的dom对象?

更新的答案:

看看你的代码,我想我可能发现了你的问题

你正在做:

var iframe = content.document.getElementById("muzich_iframe_addcontainer");
if (iframe)
{
  if (iframe.contentDocument.getElementById("div"))
  {
    this.close_all();
  }
}
muzich\u iframe\u addcontainer
是一个div,而不是iframe,因此它从来没有contentDocument。 此外,我无法通过创建xul元素使其工作。我必须创建HTMLdiv和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();
  }
}

希望这一个能为您解决问题。

I'vce在完全加载iframe后尝试执行此代码,结果是相同的。您能否发布创建和加载iframe的代码以尝试找到问题?代码:(在此版本中,不调用“start\u listener”)。Iframe创建了l。81