繁体   English   中英

返回页面中所有iframe的ID

[英]Return ID of all iframes in a page

由于我正在使用的小部件格式,我有一个页面,其中嵌入了iframe中的多个iframe。 我不会粘贴代码,因为它庞大而且笨拙,但基本上就是这样:

<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>

但是,可能会有更多 - 或更少 - iframe依赖于我使用的页面和模板。

因此,我试图从最嵌入的iframe中获取此页面中所有iframe的ID。

这有可能与jQuery? 我已经尝试了几段代码,但没有运气:

$('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"));
});

这些输出是一个ID字符串 - 不幸的是,它不是我想要控制的iframe。

提前致谢,

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

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

EDITED

这将返回一个iframe元素,该元素具有所需的id ,如果找不到所需的id ,则返回null

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;
}

请注意,在触发主窗口的onload之前,无法运行searchIds()

从最嵌入的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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM