【问题标题】:Is it possible to get document of iframe with iframe_id?是否可以使用 iframe_id 获取 iframe 文档?
【发布时间】:2026-02-02 03:40:01
【问题描述】:

AFAIK,我可以通过 window.frames['frame_name'] 实现这个目标

但是如果我只知道frame_id,是否也可以获取内容?

你可以在这里试试:

http://maishudi.com/rte-light-read-only/index.html

顺便说一句,是否可以通过iframe_id获取位置属性?

我很久以前尝试过,但失败了。

【问题讨论】:

    标签: dom iframe


    【解决方案1】:

    我认为应该这样做。它适用于(最新的)firefox、safari、camino 和opera (mac)。
    你还是应该做一些测试(尤其是在 IE 中)

    var iframeObj = document.getElementById("theIframeId");
    var documentObj = iframeObj.contentWindow || iframeObj.contentDocument;
    var location = documentObj.location.href; // I think this only works if
                                              // the content of the iframe comes 
                                              // from the same domain.
    

    【讨论】:

    • 我已经测试过它不起作用。你可以在这里测试它:maishudi.com/rte-light-read-only/index.html 使用:var iframeObj = document.getElementById("id_description"); var documentObj = iframeObj.contentWindow || iframeObj.contentDocument; alert(documentObj) ;
    • 也许是 IE?它在 Firefox 中失败。
    • tinyurl.com/oy48ul 之所以有效,是因为您使用的是 iframe_name,而不是 iframe_id..
    • 不,两种方法都经过测试。我现在编辑了它,只留下了带有 contentWindow/Document 的那个
    • 哦,是的,它有效!顺便说一句,是否可以通过 iframe_id 获取位置属性?我很久以前尝试过,但失败了。