2009-09-21 50 views
1

希望有人能够协助,但我需要从嵌套3和4级别的父表单中访问表单和iframe中的值。如何访问嵌套在DOM中的项目值?

基本上,树状分层结构(DOM)似乎是如下:

<form name="formname" ....> 
    <iframe id="refframe"> 
    <form name="formname" ...>    ==> Level 3 
     <iframe id="iframe1">     ==> Level 4 

现在我有3级(说myvalue的)一个项目的值,我想从顶级访问(父级 - 级别1)

使用JavaScript,我怎样才能从父级顶级访问此项目“myValue”?

这同样与第四级访问的项,的iframe1

任何帮助的IFRAME ID内/实例,将不胜感激。

谢谢。

基础上,我仍然不知道如何获取信息,但如果我有以下的HTML代码上面:

<div class="tabs0"> 

<iframe id="refframe" width="1210px" height="584px" frameborder="0" align="left"

overflow="auto" src="http://www.abc.com" home="" name="Site1"> 
</iframe> 

</div> 

<div class="tabs1"> 
<iframe id="refframe" width="1210px" height="584px" frameborder="0" align="left" `overflow="auto" src="http://www.def.com" name="Site2">` 

如何访问中src值tabs1 class iframe from the parent Window,ie

src =“http://www.def.com”

再次感谢

+0

请说明您是否正在处理外部域名。 – 2009-09-21 06:23:09

回答

3

iframe中的内容是否与父文档从同一个域加载?如果没有,那么由于浏览器安全限制(same domain policy),您将无法通过javascript访问它。

如果它来自同一个域,则应该可以使用getElementById而不会有任何问题。

1

只需使用id作为一个独特的值(因为它应该是 - 见例如。 here,第7.5.2节) - 然后,GetElementById只是做你想要的,而不需要你导航任何复杂的层次结构。

1
<form name="formname" .... id="form-first"> 
    <iframe id="one" src="iframe2.html"> 
    </iframe> 
</form> 


function iframeRef(frameRef) { 
    return frameRef.contentWindow ? frameRef.contentWindow.document : frameRef.contentDocument 
} 

var inside = iframeRef(document.getElementById('one')) 

var secondIframe = inside.getElementsByTagName('iframe') 
alert(secondIframe.id) // make sure theres an iframe with an id in the iframe2.html. 

您可以使用iframeRef获取iframe中的引用,并且只要继续这样做即可获取子iframe。这应该适用于IE。

+0

注意:如果iframe中的文档位于您的域上,这将*仅*工作。 – 2009-09-21 02:50:39

+0

RE:您的更新 - 如果不在您的域中,您*无法*访问iframe内容。 – 2009-09-21 06:22:38

+0

了解你在说什么,但我实际上是在Intranet类型设置中工作的,我实际上有权访问我参考的所有站点,即使我跳转到iframe中的不同站点。基于此,我是否仍然无法访问内容? – tonyf 2009-09-21 06:27:45