返回页面中所有iframe的ID

编程入门 行业动态 更新时间:2024-10-27 08:27:07
本文介绍了返回页面中所有iframe的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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>

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

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

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

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

这可以用jQuery吗?我尝试了几段代码,但没有运气:

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

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

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

提前致谢,

推荐答案

EDITED

这会返回一个 iframe 元素,其中包含想要的 id 和 null ,如果找不到想要的 id 。

This returns an iframe element which has the wanted id, and null, if the wanted id is not found.

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

Notice, that searchIds() can't be run before onload of the main window has been fired.

更多推荐

返回页面中所有iframe的ID

本文发布于:2023-10-26 10:09:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   iframe   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!