如何在iframe内访问iframe?
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有一个包含
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 anotheriframe
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 asrc
of...
and then the document (from...
) the is loaded into that iframe contains another iframe thendocument.getElementById("embedIframe")
doesn't work becauseembedIframe
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.
这样,您可以访问顶部iFrame,然后从嵌入式iframe内部嵌入的iframe。给框架1给ID,以便您可以轻松访问它。
您的问题可能是您在嵌入式iFrame内部,因此请使用文档。从此,访问顶级文档将无法正常工作。
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.