JavaScript訪問同一個頁面中的不同iframe的內容!

來源:互聯網
上載者:User
比較大一點的WEB項目一般頁面都會用到iframe,這樣如何訪問各個iframe的內容就顯得比較重要,比如登陸頁放在一個iframe中,而登陸狀態顯示頁又放在同一個頁面的另一個iframe中,那樣的話在登陸的時候就得重新整理另一個iframe中的登陸狀態顯示頁,經本人一晚上GOOGLE+親測,得如下代碼,可通過javascript來訪問或重新整理同一個頁面中不同iframe中的內容。
    本範例共3個測試檔案:index.html, ye1.html, ye2.html   各個檔案源碼如下:
index.html: 原始碼複製(IE有效)列印關於
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>JS架構測試</title>  
  6. </head>  
  7. <script language="javascript" type="text/javascript">  
  8. function fun()   
  9. {   
  10.     window.frames["ye1"].document.getElementById("con1").innerHTML = "aaa";  // 設定子表單中的內容   
  11. //  window.frames["ye1"].location.reload();    // 重新整理子表單   
  12. }   
  13. </script>  
  14. <body>  
  15. <p>  
  16. <iframe src="ye1.html" name="ye1" width="200" height="120" scrolling="auto"></iframe>  
  17. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<iframe src="ye2.html" name="ye2" width="300" height="120" scrolling="auto"></iframe>  
  18. </p>  
  19. <p>  
  20.   <input type="button" name="Submit" value="改變frm1的內容" onclick="fun()" />  
  21. </p>  
  22. <div id="main"></div>  
  23. </body>  
  24. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>JS架構測試</title></head><script language="javascript" type="text/javascript">function fun(){    window.frames["ye1"].document.getElementById("con1").innerHTML = "aaa";  // 設定子表單中的內容//window.frames["ye1"].location.reload();    // 重新整理子表單}</script><body><p><iframe src="ye1.html" name="ye1" width="200" height="120" scrolling="auto"></iframe>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<iframe src="ye2.html" name="ye2" width="300" height="120" scrolling="auto"></iframe></p><p>  <input type="button" name="Submit" value="改變frm1的內容" onclick="fun()" /></p><div id="main"></div></body></html>

ye1.html: 原始碼複製(IE有效)列印關於

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>頁面一</title>  
  6. <script>  
  7.     var i = Math.random();   
  8.     document.write(i);   
  9. </script>  
  10. </head>  
  11.   
  12. <body>  
  13. <p>頁面一</p>  
  14. <div id="con1"></div>  
  15. </body>  
  16. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>頁面一</title><script>var i = Math.random();document.write(i);</script></head><body><p>頁面一</p><div id="con1"></div></body></html>

ye1裡加上一段javascript是為了方便在測試重新整理的時候能看到效果,即每回重新整理都隨機產生一個數.
ye2.html 原始碼複製(IE有效)列印關於

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>頁面一</title>  
  6. </head>  
  7. <script language="javascript" type="text/javascript">  
  8. function fun()   
  9. {   
  10.     // window.parent.document.getElementById("main").innerHTML = "哎呀";   // 設定父表單內的內容   
  11.     // window.parent.frames["ye1"].location.reload();   // 重新整理父表單中的另一個子窗體   
  12.     window.parent.frames["ye1"].document.getElementById("con1").innerHTML = "bbb";  // 設定父表單中的另一個子表單的內容   
  13. }   
  14. </script>  
  15. <body>  
  16. <p>頁面二</p>  
  17. <p>  
  18.   <input type="button" name="Submit" value="改變頁面內容"  onclick="fun()"/>  
  19. </p>  
  20. <div id="con2"></div>  
  21. </body>  
  22. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>頁面一</title></head><script language="javascript" type="text/javascript">function fun(){// window.parent.document.getElementById("main").innerHTML = "哎呀";   // 設定父表單內的內容// window.parent.frames["ye1"].location.reload();   // 重新整理父表單中的另一個子窗體window.parent.frames["ye1"].document.getElementById("con1").innerHTML = "bbb";  // 設定父表單中的另一個子表單的內容}</script><body><p>頁面二</p><p>  <input type="button" name="Submit" value="改變頁面內容"  onclick="fun()"/></p><div id="con2"></div></body></html>

    運行index.html點擊按鈕即可看到效果,以上代碼解決如下問題:
    ① 父表單訪問或重新整理子表單
    ② 子表單訪問或重新整理父表單
    ③ 同一個父表單中的兩個子表單之間的相互訪問或重新整理
代碼經測試在IE5.5, IE6, IE7, IE8 Beta 2, FireFox, Opera, Chrome瀏覽器上測試通過!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.