开发者

Finding an iframe within another iframe?

I have the id of the parent, but not the child iframe.

Is there a way to return 开发者_如何转开发the iframe within the parent iframe?

Or, all iframes within a particular iframe?

Using jQuery to find element with $('iframe') doesn't seem to work.


If the iframe's src attribute has the same domain, protocol and port, you are set. If not, you can't do anything because of Same Origin Policy.

Assuming you are not violating the policy...

Regular JavaScript

var iframe = document.getElementsByTagName('iframe')[0],
    iframeDocument = iframe.contentWindow || iframe.contentDocument,
    internalIframes = iframeDocument.getElementsByTagName('iframe');

You need to use the || short circuit evaluation exploit as IE is different.

jQuery

var internalIframes = $('iframe:first').contents().find('iframe');


var childFrames = frames['parentId'].frames

Make that recursive and start from top.frames might suit your needs as well... and has a lot less of the DOM heavy "getElementsByTagName"

or if you are in the child i frame but you just need to get a handle on the frame, this gets you the id: window.frameElement.id

**only tested in IE :(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜