簡體   English   中英

訪問iframe內容(在firefox插件javascript中)

[英]Acess iframe content (in firefox plugin javascript)

我正在編寫一個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();
  }
}

希望這個可以為您解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM