嵌套 iframe,又名 iframe Inception [英] Nested iframes, AKA Iframe Inception

查看:27
本文介绍了嵌套 iframe,又名 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.

推荐答案

问题是,您提供的代码将不起作用,因为