从 iFrame 中获取元素 [英] Get element from within an iFrame
本文介绍了从 iFrame 中获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 中获取
?
解决方案
var iframe = document.getElementById('iframeId');var innerDoc = (iframe.contentDocument) ?iframe.contentDocument : iframe.contentWindow.document;
你可以更简单地写:
var iframe = document.getElementById('iframeId');var innerDoc = iframe.contentDocument ||iframe.contentWindow.document;
并且将返回第一个有效的内部文档.
一旦您获得内部文档,您就可以像访问当前页面上的任何元素一样访问其内部内容.(innerDoc.getElementById
...等)
重要提示:确保 iframe 位于同一个域中,否则您将无法访问其内部结构.那将是跨站点脚本.
How do you get a <div>
from within an <iframe>
?
解决方案
var iframe = document.getElementById('iframeId');
var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
You could more simply write:
var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
and the first valid inner doc will be returned.
Once you get the inner doc, you can just access its internals the same way as you would access any element on your current page. (innerDoc.getElementById
...etc.)
IMPORTANT: Make sure that the iframe is on the same domain, otherwise you can't get access to its internals. That would be cross-site scripting.
这篇关于从 iFrame 中获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文