5

Because of the widget format I'm working with I have a page which has multiple iframes embedded within iframes. I won't paste the code as it's vast and unwieldy but it is essentially just this:

<html>
    <head></head>
    <body>
        <iframe>
            <html>
                <head></head>
                <body>
                    <iframe>
                        <html>
                            <head></head>
                            <body>
                                <iframe>
                                    <html>
                                        <head></head>
                                        <body>
                                        blah
                                        </body>
                                    </html>
                                </iframe>
                            </body>
                        </html>
                    </iframe>
                </body>
            </html>
        </iframe>
    </body>
</html>

However, there may be more - or less - iframes dependent upon the page and template I use.

I'm trying to therefore get the ID of all of the iframes in this page, from within the most-embedded iframe.

Is this possible with jQuery? I've tried a few snippets of code but had no luck:

$('iframe', window.parent.document).each(function() {
    console.log($(this).attr("id"));
});

$('iframe', parent.document).each(function() {
    console.log($(this).attr("id"));
});

$('iframe').each(function() {
    console.log($(this).attr("id"));
});

The output of these is a single ID string - and unfortunately it's not the iframe I'm looking to control.

Thanks in advance,

4

3 回答 3

3

已编辑

这将返回一个iframe具有所需 的元素idnull如果id未找到所需的 ,则返回。

function searchIds(wantedId) {
    var idArray = [], n,
        search = function (iframes) {
            var n;
            for (n = 0; n < iframes.length; n++) {
                if (iframes[n].frames.length > 0) {
                    search(iframes[n].frames);
                }
                idArray.push(iframes[n].frameElement.id);
                idArray.push(iframes[n].frameElement);
            }
        };
    search(window.top.frames);
    for (n = 0; n < idArray.length; n += 2) {
        if (idArray[n] === wantedId) {
            return idArray[n + 1];
        }
    }
    return null;
}

注意,在主窗口被触发searchIds()之前不能运行。onload

于 2013-09-19T08:37:23.300 回答
2

我不相信你可以直接引用 iframe 的孩子。您将需要使用它的.contents()调用递归搜索每个 iframe。

据我所知,这只有在不违反同源策略的情况下才有效(即 iframe 必须指向同一个域)。

于 2013-09-19T07:59:41.843 回答
0

来自最嵌入的 iframe:

var ids = (function up( context, ids ){ 
   ids = ids || []; 
   // proceed if we're not at the top window yet
   if( context !== window.top){
      // point context to parent window (or top if no parent).
      context = context.parent || window.top; 
      // get the id of the first iframe in parent window; 
      // this will break if there are sibling iframes
      ids.push(context.document.getElementsByTagName('iframe')[0].id); 

      // recursive call to traverse parents          
      return up(context, ids); 
   } else {
      // otherwise return the list of ids
      return ids; 
   }
   }(this)); // 'this' is the initial context - current window

   console.log( ids );
于 2013-09-19T08:32:27.913 回答