Home > Web Front-end > JS Tutorial > A brief analysis of the interoperability of JavaScript iframes_javascript skills

A brief analysis of the interoperability of JavaScript iframes_javascript skills

WBOY
Release: 2016-05-16 18:44:39
Original
1238 people have browsed it

The iframe element is a document within a document, or like a floating frame. The operation of iframe has always been a difficult point, and there are many articles on this aspect on the Internet. Below I will organize my notes and record them based on my own experience.
1. Page
Three pages: parent page and two child pages. The two child pages are in two iframes of the parent page.
1. Parent page MainForm.aspx

<code jquery1255503204984="2"><span class="pun"><%</SPAN><SPAN class=pln>@ </SPAN><SPAN class=typ>Page</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>Language</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"C#"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>AutoEventWireup</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"true"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>CodeBehind</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"MainForm.aspx.cs"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>Inherits</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"Test.MainForm"</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>%></span><span class="pln"><br><br></span><span class="dec"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></span><span class="pln"><br></span><span class="pun"><</SPAN><SPAN class=tag>html</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>xmlns</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"http://www.w3.org/1999/xhtml"</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"><</SPAN><SPAN class=tag>head</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>runat</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"server"</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>title</SPAN><SPAN class=pun>></</SPAN><SPAN class=tag>title</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"></</SPAN><SPAN class=tag>head</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"><</SPAN><SPAN class=tag>body</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>form</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>id</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"form1"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>runat</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"server"</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>div</SPAN><SPAN class=pun>></span><span class="pln"><br>        </span><span class="pun"><</SPAN><SPAN class=tag>ul</SPAN><SPAN class=pun>></span><span class="pln"><br>            </span><span class="pun"><</SPAN><SPAN class=tag>li</SPAN><SPAN class=pun>></span><span class="pln"><br>                父页面(文本失去焦点可看结果):</span><span class="pun"><</SPAN><SPAN class=tag>input</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>id</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"txtParent"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>name</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"txtParent"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>type</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"text"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>value</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"parent"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>onblur</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"</SPAN><SPAN class=str>iframeTest()</SPAN><SPAN class=atv>"</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>/></span><span class="pln"><br>            </span><span class="pun"></</SPAN><SPAN class=tag>li</SPAN><SPAN class=pun>></span><span class="pln"><br>            </span><span class="pun"><</SPAN><SPAN class=tag>li</SPAN><SPAN class=pun>></span><span class="pln"><br>                </span><span class="pun"><</SPAN><SPAN class=tag>iframe</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>src</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"FrameA.aspx"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>id</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"iframeA"</SPAN><SPAN class=pun>></</SPAN><SPAN class=tag>iframe</SPAN><SPAN class=pun>></span><span class="pln"><br>            </span><span class="pun"></</SPAN><SPAN class=tag>li</SPAN><SPAN class=pun>></span><span class="pln"><br>            </span><span class="pun"><</SPAN><SPAN class=tag>li</SPAN><SPAN class=pun>></span><span class="pln"><br>                </span><span class="pun"><</SPAN><SPAN class=tag>iframe</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>src</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"FrameB.aspx"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>id</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"iframeB"</SPAN><SPAN class=pun>></</SPAN><SPAN class=tag>iframe</SPAN><SPAN class=pun>></span><span class="pln"><br>            </span><span class="pun"></</SPAN><SPAN class=tag>li</SPAN><SPAN class=pun>></span><span class="pln"><br>        </span><span class="pun"></</SPAN><SPAN class=tag>ul</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"></</SPAN><SPAN class=tag>div</SPAN><SPAN class=pun>></span><span class="pln"><br><br>    </span><span class="pun"><</SPAN><SPAN class=tag>script</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>type</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"text/</SPAN><STRONG><SPAN class=atv><FONT color=#810081>javascript</FONT></SPAN></STRONG><SPAN class=atv>"</SPAN><SPAN class=pun>></span><span class="pln"><br><br>        </span><span class="kwd">function</span><span class="pln"> iframeTest</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br><br>        </span><span class="pun">}</span><span class="pln"><br><br>    </span><span class="pun"></</SPAN><SPAN class=tag>script</SPAN><SPAN class=pun>></span><span class="pln"><br><br>    </span><span class="pun"></</SPAN><SPAN class=tag>form</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"></</SPAN><SPAN class=tag>body</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"></</SPAN><SPAN class=tag>html</SPAN><SPAN class=pun>></span></code>
Copy after login

2. Sub-page A

<code jquery1255503204984="3"><span class="pun"><%</SPAN><SPAN class=pln>@ </SPAN><SPAN class=typ>Page</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>Language</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"C#"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>AutoEventWireup</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"true"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>CodeBehind</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"FrameA.aspx.cs"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>Inherits</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"Test.FrameA"</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>%></span><span class="pln"><br><br></span><span class="dec"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></span><span class="pln"><br></span><span class="pun"><</SPAN><SPAN class=tag>html</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>xmlns</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"http://www.w3.org/1999/xhtml"</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"><</SPAN><SPAN class=tag>head</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>runat</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"server"</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>title</SPAN><SPAN class=pun>></</SPAN><SPAN class=tag>title</SPAN><SPAN class=pun>></span><span class="pln"><br><br>    </span><span class="pun"><</SPAN><SPAN class=tag>script</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>type</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"text/javascript"</SPAN><SPAN class=pun>></span><span class="pln"><br><br>        </span><span class="com">//子页面与父页面之间的操作  </span><span class="pln"><br>        </span><span class="kwd">function</span><span class="pln"> getParent</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br><br>        </span><span class="pun">}</span><span class="pln"><br><br>        </span><span class="com">//当前子页面与另外一个子页面之间的操作</span><span class="pln"><br>        </span><span class="kwd">function</span><span class="pln"> getAnotherChild</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br><br>        </span><span class="pun">}</span><span class="pln"><br>    </span><span class="pun"></</SPAN><SPAN class=tag>script</SPAN><SPAN class=pun>></span><span class="pln"><br><br></span><span class="pun"></</SPAN><SPAN class=tag>head</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"><</SPAN><SPAN class=tag>body</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>form</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>id</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"form1"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>runat</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"server"</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>div</SPAN><SPAN class=pun>></span><span class="pln"><br>         子页面1(文本失去焦点可看结果):</span><span class="pun"><</SPAN><SPAN class=tag>input</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>id</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"txtUserName"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>name</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"txtUserName"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>type</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"text"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>value</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"jeff wong"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>onblur</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"</SPAN><SPAN class=str>getParent()</SPAN><SPAN class=atv>"</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>/></span><span class="pln"><br>    </span><span class="pun"></</SPAN><SPAN class=tag>div</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"></</SPAN><SPAN class=tag>form</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"></</SPAN><SPAN class=tag>body</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"></</SPAN><SPAN class=tag>html</SPAN><SPAN class=pun>></span><span class="pln"><br></span></code>
Copy after login

3. Sub-page B

<code jquery1255503204984="4"><span class="pun"><%</SPAN><SPAN class=pln>@ </SPAN><SPAN class=typ>Page</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>Language</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"C#"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>AutoEventWireup</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"true"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>CodeBehind</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"FrameB.aspx.cs"</SPAN><SPAN class=pln> </SPAN><SPAN class=typ>Inherits</SPAN><SPAN class=pun>=</SPAN><SPAN class=str>"Test.FrameB"</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>%></span><span class="pln"><br><br></span><span class="dec"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></span><span class="pln"><br><br></span><span class="pun"><</SPAN><SPAN class=tag>html</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>xmlns</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"http://www.w3.org/1999/xhtml"</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"><</SPAN><SPAN class=tag>head</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>runat</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"server"</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>title</SPAN><SPAN class=pun>></</SPAN><SPAN class=tag>title</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"></</SPAN><SPAN class=tag>head</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"><</SPAN><SPAN class=tag>body</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>form</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>id</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"form1"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>runat</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"server"</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"><</SPAN><SPAN class=tag>div</SPAN><SPAN class=pun>></span><span class="pln"><br>        子页面2(文本失去焦点可看结果):</span><span class="pun"><</SPAN><SPAN class=tag>input</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>id</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"txtUserNameB"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>name</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"txtUserNameB"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>type</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"text"</SPAN><SPAN class=pln>  </SPAN><SPAN class=atn>value</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"jeffery zhao"</SPAN><SPAN class=pun>/></span><span class="pln"><br>    </span><span class="pun"></</SPAN><SPAN class=tag>div</SPAN><SPAN class=pun>></span><span class="pln"><br>    </span><span class="pun"></</SPAN><SPAN class=tag>form</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"></</SPAN><SPAN class=tag>body</SPAN><SPAN class=pun>></span><span class="pln"><br></span><span class="pun"></</SPAN><SPAN class=tag>html</SPAN><SPAN class=pun>></span></code>
Copy after login


2. Operation
1. Parent page operation child page
This must undoubtedly be implemented in the parent page MainForm.aspx file. We modify the javascript function iframeTest of the parent page to see how the parent page obtains and operates sub-page elements:

<code jquery1255503204984="5"><span class="pln">        </span><span class="kwd">function</span><span class="pln"> iframeTest</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>            </span><span class="kwd">var</span><span class="pln"> frame1 </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"iframeA"</span><span class="pun">);</span><span class="pln"><br>            </span><span class="kwd">var</span><span class="pln"> frame2 </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"iframeB"</span><span class="pun">);</span><span class="pln"><br><br>            </span><span class="kwd">var</span><span class="pln"> frameA </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">frames</span><span class="pun">[</span><span class="str">"iframeA"</span><span class="pun">];</span><span class="pln"> </span><span class="com">//等价于 var frameAa = document.frames.iframeA;</span><span class="pln"><br>            </span><span class="kwd">var</span><span class="pln"> frameB </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">frames</span><span class="pun">[</span><span class="str">"iframeB"</span><span class="pun">];</span><span class="pln"> </span><span class="com">//等价于 var frameBb = document.frames.iframeB;</span><span class="pln"><br><br>            </span><span class="com">//**********************************************</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">frame1 </span><span class="pun">==</span><span class="pln"> frameA</span><span class="pun">);</span><span class="pln"> </span><span class="com">//false</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">frame2 </span><span class="pun">==</span><span class="pln"> frameB</span><span class="pun">);</span><span class="pln"> </span><span class="com">//false</span><span class="pln"><br><br>            alert</span><span class="pun">(</span><span class="pln">frame1</span><span class="pun">.</span><span class="pln">src</span><span class="pun">);</span><span class="pln"> </span><span class="com">//FrameA.aspx</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">frame1</span><span class="pun">.</span><span class="pln">location</span><span class="pun">);</span><span class="pln"> </span><span class="com">//undefined</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">frameA</span><span class="pun">.</span><span class="pln">src</span><span class="pun">);</span><span class="pln"> </span><span class="com">//undefined</span><span class="pln"><br><br>            alert</span><span class="pun">(</span><span class="pln">frameA</span><span class="pun">.</span><span class="pln">location</span><span class="pun">);</span><span class="pln"> </span><span class="com">//location</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">frameA</span><span class="pun">.</span><span class="pln">document</span><span class="pun">.</span><span class="pln">location</span><span class="pun">);</span><span class="pln"><br><br>            alert</span><span class="pun">(</span><span class="pln">frame1</span><span class="pun">.</span><span class="pln">document</span><span class="pun">.</span><span class="pln">body</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"> </span><span class="com">//这里返回的是MainForm.aspx的body里的innerHTML</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">frame1</span><span class="pun">.</span><span class="pln">document</span><span class="pun">.</span><span class="pln">documentElement</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"> </span><span class="com">//这里返回的是MainForm.aspx的html里的innerHTML</span><span class="pln"><br><br>            alert</span><span class="pun">(</span><span class="pln">frameA</span><span class="pun">.</span><span class="pln">document</span><span class="pun">.</span><span class="pln">body</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"> </span><span class="com">//这里返回的是FrameA.aspx的body里的innerHTML</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">frameA</span><span class="pun">.</span><span class="pln">document</span><span class="pun">.</span><span class="pln">documentElement</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"> </span><span class="com">//这里返回的是FrameA.aspx的html里的innerHTML</span><span class="pln"><br><br>            </span><span class="com">//**********************************************   </span><span class="pln"><br>            </span><span class="kwd">var</span><span class="pln"> childFrameDoc </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">undefined</span><span class="pun">;</span><span class="pln"><br><br>            </span><span class="com">//取FrameA.aspx内的input文本</span><span class="pln"><br>            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">document</span><span class="pun">.</span><span class="pln">all</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="com">//IE</span><span class="pln"><br>                childFrameDoc </span><span class="pun">=</span><span class="pln"> frameA</span><span class="pun">.</span><span class="pln">document</span><span class="pun">;</span><span class="pln"> </span><span class="com">//*** 如果是frame1,就取不到FrameA.aspx页面里的input ***</span><span class="pln"><br>            </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="com">//Firefox</span><span class="pln"><br>                childFrameDoc </span><span class="pun">=</span><span class="pln"> frameA</span><span class="pun">.</span><span class="pln">contentDocument</span><span class="pun">;</span><span class="pln"><br>            </span><span class="pun">}</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">childFrameDoc</span><span class="pun">.</span><span class="pln">body</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"><br><br>            </span><span class="kwd">var</span><span class="pln"> childTxt </span><span class="pun">=</span><span class="pln"> childFrameDoc</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"txtUserName"</span><span class="pun">);</span><span class="pln"><br>            </span><span class="kwd">var</span><span class="pln"> childTxtByName </span><span class="pun">=</span><span class="pln"> childFrameDoc</span><span class="pun">.</span><span class="pln">getElementsByName</span><span class="pun">(</span><span class="str">"txtUserName"</span><span class="pun">);</span><span class="pln"><br><br>            alert</span><span class="pun">(</span><span class="pln">childTxt </span><span class="pun">==</span><span class="pln"> childTxtByName</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]);</span><span class="pln"> </span><span class="com">//true</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">childTxt</span><span class="pun">.</span><span class="pln">value</span><span class="pun">);</span><span class="pln"> </span><span class="com">//jeff wong</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">childTxtByName</span><span class="pun">[</span><span class="lit">0</span><span class="pun">].</span><span class="pln">value</span><span class="pun">);</span><span class="pln"> </span><span class="com">// jeff wong</span><span class="pln"><br><br><br>            </span><span class="com">//取FrameB.aspx内的input文本</span><span class="pln"><br>            childFrameDoc </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">undefined</span><span class="pun">;</span><span class="pln"><br>            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">document</span><span class="pun">.</span><span class="pln">all</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="com">//IE</span><span class="pln"><br>                childFrameDoc </span><span class="pun">=</span><span class="pln"> frameB</span><span class="pun">.</span><span class="pln">document</span><span class="pun">;</span><span class="pln"><br>            </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="com">//Firefox</span><span class="pln"><br>                childFrameDoc </span><span class="pun">=</span><span class="pln"> frameB</span><span class="pun">.</span><span class="pln">contentDocument</span><span class="pun">;</span><span class="pln"><br>            </span><span class="pun">}</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">childFrameDoc</span><span class="pun">.</span><span class="pln">body</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"><br><br>            </span><span class="kwd">var</span><span class="pln"> childTxt </span><span class="pun">=</span><span class="pln"> childFrameDoc</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"txtUserNameB"</span><span class="pun">);</span><span class="pln"><br>            </span><span class="kwd">var</span><span class="pln"> childTxtByName </span><span class="pun">=</span><span class="pln"> childFrameDoc</span><span class="pun">.</span><span class="pln">getElementsByName</span><span class="pun">(</span><span class="str">"txtUserNameB"</span><span class="pun">);</span><span class="pln"><br><br>            alert</span><span class="pun">(</span><span class="pln">childTxt </span><span class="pun">==</span><span class="pln"> childTxtByName</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]);</span><span class="pln"> </span><span class="com">//true</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">childTxt</span><span class="pun">.</span><span class="pln">value</span><span class="pun">);</span><span class="pln"> </span><span class="com">//jeffery zhao</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="pln">childTxtByName</span><span class="pun">[</span><span class="lit">0</span><span class="pun">].</span><span class="pln">value</span><span class="pun">);</span><span class="pln"> </span><span class="com">// jeffery zhao</span><span class="pln"><br><br>        </span><span class="pun">}</span></code>
Copy after login

Summary:
a. Through the object model of the page where the iframe object is located (obtained through document.getElementById("iframeId")), you can access the properties of the iframe object, but not its content.
b. The frames collection provides access to iframe content (obtained through document.frames["iframeName"]). Normally we use the frames collection to read and write the elements contained in the iframe.
c. If you only want to change the src or border, scrolling and other attributes of the iframe (which is not the same concept as property, property cannot be written in the tag, such as: scrollHeight, innerHTML, etc.), you need to use the document.getElementById method .
d. If you want to use the function or dom element in the iframe (for example, if you want to get the content of the document.body of the iframe), the variable must be passed through the frames collection. Because it takes a complete DOM model and gets it through the id The method just takes out an object.
e. If the dom model of the iframe is called when the iframe page is not fully loaded, an error will occur.

2. The child page operates the parent Page
Here we take the subpage FrameA.aspx as an example. In subpage A, we get the dom element of MainForm.aspx through the function getParent, and then just follow the normal dom element operation:

<code jquery1255503204984="6"><span class="pln">        </span><span class="com">//子页面与父页面之间的操作  </span><span class="pln"><br>        </span><span class="kwd">function</span><span class="pln"> getParent</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">self</span><span class="pln"> </span><span class="pun">!=</span><span class="pln"> top</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>                </span><span class="kwd">var</span><span class="pln"> oDoc </span><span class="pun">=</span><span class="pln"> top</span><span class="pun">.</span><span class="pln">parent</span><span class="pun">.</span><span class="pln">document</span><span class="pun">;</span><span class="pln"><br>                alert</span><span class="pun">(</span><span class="pln">oDoc</span><span class="pun">.</span><span class="pln">body</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"> <br>                alert</span><span class="pun">(</span><span class="pln">oDoc</span><span class="pun">.</span><span class="pln">documentElement</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"> <br>                alert</span><span class="pun">(</span><span class="pln">oDoc</span><span class="pun">.</span><span class="pln">frames</span><span class="pun">.</span><span class="pln">length</span><span class="pun">);</span><span class="pln"> </span><span class="com">//返回结果:2  表明父页面有两个iframe</span><span class="pln"><br><br>                </span><span class="com">//操作父页面的文本框</span><span class="pln"><br>                </span><span class="kwd">var</span><span class="pln"> oTxt </span><span class="pun">=</span><span class="pln"> oDoc</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"txtParent"</span><span class="pun">);</span><span class="pln"><br>                alert</span><span class="pun">(</span><span class="pln">oTxt</span><span class="pun">.</span><span class="pln">value</span><span class="pun">);</span><span class="pln"><br><br>            </span><span class="pun">}</span><span class="pln"><br>            </span><span class="kwd">else</span><span class="pln"> alert</span><span class="pun">(</span><span class="str">"不在框架中"</span><span class="pun">);</span><span class="pln"><br>        </span><span class="pun">}</span></code>
Copy after login


3. Sub-page operation Sub-page
This also requires the use of the main page to indirectly obtain another sub-page. Here we also take subpage A as an example. Through the main page, we indirectly obtain sub-page B, and then sub-page A completes the control and operation of sub-page B like normal dom operations.
First modify the getAnotherChild() function:

<code jquery1255503204984="7"><span class="pln">        </span><span class="com">//当前子页面与另外一个子页面之间的操作</span><span class="pln"><br>        </span><span class="kwd">function</span><span class="pln"> getAnotherChild</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>            alert</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">location</span><span class="pun">.</span><span class="pln">href</span><span class="pun">);</span><span class="pln"> </span><span class="com">//当前页面的url</span><span class="pln"><br><br>            </span><span class="com">//通过父页面,间接获取另外一个子页面</span><span class="pln"><br>            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">self</span><span class="pln"> </span><span class="pun">!=</span><span class="pln"> top</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>                </span><span class="kwd">var</span><span class="pln"> oDoc </span><span class="pun">=</span><span class="pln"> top</span><span class="pun">.</span><span class="pln">parent</span><span class="pun">.</span><span class="pln">document</span><span class="pun">;</span><span class="pln"><br>                </span><span class="kwd">var</span><span class="pln"> oAnotherFrame </span><span class="pun">=</span><span class="pln"> oDoc</span><span class="pun">.</span><span class="pln">frames</span><span class="pun">[</span><span class="str">"iframeB"</span><span class="pun">];</span><span class="pln"> </span><span class="com">//返回另外一个iframe</span><span class="pln"><br><br>                alert</span><span class="pun">(</span><span class="pln">oAnotherFrame</span><span class="pun">.</span><span class="pln">location</span><span class="pun">);</span><span class="pln"><br>                alert</span><span class="pun">(</span><span class="pln">oAnotherFrame</span><span class="pun">.</span><span class="pln">document</span><span class="pun">.</span><span class="pln">body</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"><br>                alert</span><span class="pun">(</span><span class="pln">oAnotherFrame</span><span class="pun">.</span><span class="pln">document</span><span class="pun">.</span><span class="pln">documentElement</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">);</span><span class="pln"><br><br>                </span><span class="kwd">var</span><span class="pln"> oTxt </span><span class="pun">=</span><span class="pln"> oAnotherFrame</span><span class="pun">.</span><span class="pln">document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"txtUserNameB"</span><span class="pun">);</span><span class="pln"><br>                alert</span><span class="pun">(</span><span class="pln">oTxt</span><span class="pun">.</span><span class="pln">value</span><span class="pun">);</span><span class="pln"> </span><span class="com">//jeffery zhao</span><span class="pln"><br>            </span><span class="pun">}</span><span class="pln"><br>        </span><span class="pun">}</span><span class="pln"><br></span></code>
Copy after login

Note: After modifying the getAnotherChild() function, then change the onblur event of textUserName in subpage A (FrameA.aspx) to onblur="getAnotherChild()" to see the result.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template