由于我正在使用的小部件格式,我有一个页面,其中嵌入了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()$ c在主窗口的 onload 被触发之前,无法运行$ c>。
Notice, that searchIds() can't be run before onload of the main window has been fired.
更多推荐
返回页面中所有iframe的ID
发布评论