通过getElementById或其他技术从iFrame中找到子元素? [英] Finding child element from iFrame via getElementById or other technique?

查看:271
本文介绍了通过getElementById或其他技术从iFrame中找到子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个iframe,需要从中获取我具有其ID的子元素.我想做这样的事情:

I have an iframe on my page, and need to get a child element from it whose ID I have. I'd like to do something like this:

document.getElementById('iframeID').getElementById('child element ID')

我愿意就如何实现这一目标提出建议.不幸的是,我无法访问此页面上的jQuery或其他方便的库.

I'm open to suggestions on how to accomplish this. Unfortunately I don't have access to jQuery or other handy libraries on this page.

推荐答案

您首先需要访问iframe文档,这是一种跨浏览器的方法:

You first need to access the iframe document, here is a cross-browser way to do it:

var ifr=document.getElementById('iframeID');
var doc=ifr.contentWindow||ifr.contentDocument;
if (doc.document) doc=doc.document;

然后您可以访问iframe内容:

You can then reach the iframe content:

doc.getElementById('child element ID')

请注意:由于相同来源政策,因此该功能仅在同一域内有效.

Note: this will only work within a same domain, due to the same origin policy.

这篇关于通过getElementById或其他技术从iFrame中找到子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆