如何在iframe内访问iframe?

发布于 2025-02-06 09:14:15 字数 613 浏览 0 评论 0原文

<iframe src="...">
    <iframe id="embedIframe" src="...">
    </iframe>
</iframe>

我想选择一个具有“嵌入式” ID的iframe元素。

我尝试了开发人员工具中的控制台窗口中的document.getElementById(“ embediframe”)

但这返回一个零值。

奇怪的是,如果我直接在带有鼠标的Chrome Developer工具元素选项卡中单击“嵌入”是输出。

https://i.sstatic.net/bxrnd.png

我正在使用React。

React找不到document.getElementById(“ embediframe”)

如何一次访问IFRAME ID“嵌入式”?

<iframe src="...">
    <iframe id="embedIframe" src="...">
    </iframe>
</iframe>

I want to select an Iframe element with an Id of "embedIframe".

I tried document.getElementById("embedIframe") in the console window in developer tools.

But this returns a null value.

The strange thing is that if I directly click "embedIframe" in the Chrome Developer Tools element tab with the mouse, then return to the console window and type document.getElementById("embedIframe"), a normal value is output.

https://i.sstatic.net/bxRnD.png

I'm using react.

React doesn't find document.getElementById("embedIframe") either.

How can i access to Iframe id "embedIframe" at once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤城病女 2025-02-13 09:14:15

如果您有一个包含iframe的文档,并且在同一文档中 中的,您有另一个iframe作为第一个的子元素…,则是您的html无效,您不能那样做。

如果浏览器不支持IFRAME,则iFrames的孩子过去是渲染的替代内容,但这已被淘汰,不再允许孩子。


如果您的iframe src加载到该iframe中包含另一个iframe,然后document.getElementById(“ embediframe”)不起作用,因为embediframe不是的一部分文档。

您需要在当前文档中获取iFrame,然后将文档属于该帧,然后搜索您想要的iframe文档。

If you have a document containing an iframe, and in the same document you have another iframe as a child element of the first … then your HTML is invalid and you can't do that.

Children of iframes used to be alternative content to render if the browser didn't support iframes, but that has been phased out and iframes are no longer allowed children.


If you have an iframe with a src of ... and then the document (from ...) the is loaded into that iframe contains another iframe then document.getElementById("embedIframe") doesn't work because embedIframe isn't part of that document.

You need to get the iframe in the current document, then get the document belonging to that frame, and then search that document for the iframe you want.

〗斷ホ乔殘χμё〖 2025-02-13 09:14:15
var frame1 = top.document.getElementById("frame1");
var frame2 = frame1.contentWindow.document.getElementById("frame2");

frame2.document.getElementById("foo");
frame2.src = 'http://....';

这样,您可以访问顶部iFrame,然后从嵌入式iframe内部嵌入的iframe。给框架1给ID,以便您可以轻松访问它。

您的问题可能是您在嵌入式iFrame内部,因此请使用文档。从此,访问顶级文档将无法正常工作。

var frame1 = top.document.getElementById("frame1");
var frame2 = frame1.contentWindow.document.getElementById("frame2");

frame2.document.getElementById("foo");
frame2.src = 'http://....';

Like this you can access the top iframe and then the embedded iframe also from within the embedded iframe. Give an id to frame1 so you can easily access it.

Your issue might be that you are inside the embedded iframe, so using document. from within that, to access the top doc, won't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文