国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript子窗口調用父窗口變量和函數的方法

2024-05-06 16:24:34
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JavaScript子窗口調用父窗口變量和函數的方法,涉及JavaScript窗口調用的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了JavaScript子窗口調用父窗口變量和函數的方法。分享給大家供大家參考。具體如下:

示例1:子窗口是新打開的窗口

父窗口:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns=" http://www.w3.org/1999/xhtml"
  4. <head> 
  5. <title>parent</title> 
  6. <script type="text/javascript"
  7. var parentPara='parent'
  8. function parentFunction() { 
  9. alert(parentPara); 
  10. </script> 
  11. </head> 
  12. <body> 
  13. <button onclick="parentFunction()">顯示變量值</button> 
  14. <button onclick="window.open('child.html')">打開新窗口</button> 
  15. </body> 
  16. </html> 

子窗口:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns=" http://www.w3.org/1999/xhtml"
  4. <head> 
  5. <title>child</title> 
  6. <script type="text/javascript"
  7. function modify() { 
  8. opener.parentPara='child'
  9. </script> 
  10. </head> 
  11. <body> 
  12. <button onclick="opener.parentFunction()">調用父頁面的方法</button> 
  13. <button onclick="modify()">更改父頁面中變量的值</button>  
  14. </body> 
  15. </html> 

只要在變量和函數前面加opener就可以訪問指定資源了。

但是當父窗口被關閉時,再如此使用會報一個錯:"被調用的對象已與其客戶端斷開連接。"

示例2:子頁面是父頁面的一個iframe

父頁面:

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns=" http://www.w3.org/1999/xhtml"
  4. <head> 
  5. <title>parent</title> 
  6. <script type="text/javascript"
  7. function fill() { 
  8. //alert(frame1.window.childPara); //顯示frame1內的變量值 
  9. var str=document.getElementById('txt1').value; //獲得文本框內輸入的值 
  10. frame1.window.div1.innerHTML=str; //將值填入子頁面的一個div中 
  11. </script> 
  12. </head> 
  13. <body> 
  14. <div style="background-color:yellow;width:300px;height:300px;"
  15. 父頁面 
  16. <iframe id="frame1" src="child.html" frameborder="0" scrolling="no" width="120px" height="120px"></iframe> 
  17. <br/><br/><br/><br/> 
  18. <input id="txt1" type="text"/> 
  19. <button onclick="fill()">將文本框內值填充入子界面</button> 
  20. </div> 
  21. </body> 
  22. </html> 

子頁面:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns=" http://www.w3.org/1999/xhtml"
  4. <head> 
  5. <title>child</title> 
  6. <script type="text/javascript"
  7. function fun() { 
  8. parent.fill(); 
  9. </script> 
  10. </head> 
  11. <body> 
  12. <div style="background-color:lightblue;width:100px;height:100px;"
  13. <b>子頁面</b><br/> 
  14. <a href="#" onclick="fun()">同父頁面按鈕</a> 
  15. <div id="div1" style="color:red;"
  16. </div> 
  17. </div> 
  18. </body> 
  19. </html> 

小發現:刷新父頁面時,其中的iframe也會隨之刷新;刷新iframe時,父頁面不會刷新。

希望本文所述對大家的JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 巴彦县| 隆子县| 富民县| 达拉特旗| 罗平县| 松溪县| 迁西县| 轮台县| 五大连池市| 江阴市| 忻城县| 革吉县| 五家渠市| 罗平县| 汝阳县| 大田县| 南皮县| 当阳市| 奉贤区| 茂名市| 正镶白旗| 巴南区| 江阴市| 钟山县| 广德县| 古浪县| 安新县| 长治市| 盘山县| 嘉兴市| 河间市| 广安市| 铜山县| 台北县| 越西县| 莱西市| 扬中市| 永春县| 奈曼旗| 宁波市| 宁武县|