嵌套 iframe,又名 iframe Inception [英] Nested iframes, AKA Iframe Inception
问题描述
使用 jQuery 我试图访问 div id="element".
Using jQuery I am trying to access div id="element".
<body>
<iframe id="uploads">
<iframe>
<div id="element">...</div>
</iframe>
</iframe>
</body>
所有 iframe 都在同一个域中,没有 www/非 www 问题.
All iframes are on the same domain with no www / non-www issues.
我已经成功选择了第一个 iframe 中的元素,但没有选择第二个嵌套的 iframe.
I have successfully selected elements within the first iframe but not the second nested iframe.
我尝试了一些东西,这是最近的(也是一次非常绝望的尝试).
I have tried a few things, this is the most recent (and a pretty desperate attempt).
var iframe = jQuery('#upload').contents();
var iframeInner = jQuery(iframe).find('iframe').contents();
var iframeContent = jQuery(iframeInner).contents().find('#element');
// iframeContent is null
为了排除计时问题,我使用了点击事件并等待了一段时间.
To rule out a timing issue I used a click event and waited a while.
jQuery().click(function(){
var iframe = jQuery('#upload').contents().find('iframe');
console.log(iframe.find('#element')); // [] null
});
有什么想法吗?谢谢.
更新:我可以像这样选择第二个 iframe...
Update: I can select the second iframe like so...
var iframe = jQuery('#upload').contents().find('iframe');
现在的问题似乎是 src 是空的,因为 iframe 是用 javascript 生成的.所以选择了 iframe 但内容长度为 0.
The problem now seems to be that the src is empty as the iframe is generated with javascript. So the iframe is selected but the content length is 0.
推荐答案
问题是,您提供的代码将不起作用,因为 元素必须具有src"属性,比如:
Thing is, the code you provided won't work because the <iframe>
element has to have a "src" property, like:
<iframe id="uploads" src="http://domain/page.html"></iframe>
使用.contents()
获取内容就可以了:
It's ok to use .contents()
to get the content:
$('#uploads).contents()
将让您访问第二个 iframe,但如果该 iframe 是INSIDE",则 http://domain/page.html
记录加载的#uploads iframe.
$('#uploads).contents()
will give you access to the second iframe, but if that iframe is "INSIDE" the http://domain/page.html
document the #uploads iframe loaded.
为了测试我是否正确,我创建了 3 个名为 main.html、iframe.html 和 noframe.html 的 html 文件,然后选择了 div#element 就好了:
To test I'm right about this, I created 3 html files named main.html, iframe.html and noframe.html and then selected the div#element just fine with:
$('#uploads').contents().find('iframe').contents().find('#element');
由于您需要等待 iframe 加载资源,因此会出现元素不可用的延迟.此外,所有 iframe 都必须在同一个域中.
There WILL be a delay in which the element will not be available since you need to wait for the iframe to load the resource. Also, all iframes have to be on the same domain.
希望这有助于...
这是我使用的 3 个文件的 html(用您的域和 url 替换src"属性):
Here goes the html for the 3 files I used (replace the "src" attributes with your domain and url):
main.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>main.html example</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function () {
console.log( $('#uploads').contents().find('iframe').contents().find('#element') ); // nothing at first
setTimeout( function () {
console.log( $('#uploads').contents().find('iframe').contents().find('#element') ); // wait and you'll have it
}, 2000 );
});
</script>
</head>
<body>
<iframe id="uploads" src="http://192.168.1.70/test/iframe.html"></iframe>
</body>
iframe.html
iframe.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>iframe.html example</title>
</head>
<body>
<iframe src="http://192.168.1.70/test/noframe.html"></iframe>
</body>
noframe.html
noframe.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>noframe.html example</title>
</head>
<body>
<div id="element">some content</div>
</body>
这篇关于嵌套 iframe,又名 iframe Inception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!