JavaScript中父子頁面資料互動執行個體詳解

來源:互聯網
上載者:User

js中關於父子頁面資料互動一些寫法

1.window.frames["test"].document.getElementById(‘menu’);
2.//由於所有的函數都存放在window對象裡面,可去掉開頭的window:
3.frames["test"].document.getElementById(‘menu’);
4.//在瀏覽器中,幀的name屬性被預設等同於子頁面的window對象,因此可以進一步簡寫:
5.test.document.getElementById(‘menu’);
window.frames["test"].document.getElementById('menu');
//由於所有的函數都存放在window對象裡面,可去掉開頭的window:
frames["test"].document.getElementById('menu');
//在瀏覽器中,幀的name屬性被預設等同於子頁面的window對象,因此可以進一步簡寫:
test.document.getElementById('menu');2.2 父頁面訪問子頁面函數或對象。子頁面的函數和對象都在其window對象裡,同上,關鍵是擷取該對象。

Java代碼

1.//假如child.html定義了showMesg函數,需要在父中調用,則這樣寫
2.window.frames['test'].showMesg();
3.//簡寫形式
4.test.showMesg();
5.//同理,對象也是如此訪問
6.alert(test.person);
//假如child.html定義了showMesg函數,需要在父中調用,則這樣寫
window.frames['test'].showMesg();
//簡寫形式
test.showMesg();
//同理,對象也是如此訪問
alert(test.person);

例子

父視窗:a1.html

 代碼如下 複製代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>a.html</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <script language="JavaScript">
 function openWin(){
  window.open("./a2.html","_blank","height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
 }
 function setValues(id,name){
  document.getElementById("cid").value = id;
  document.getElementById("cname").value = name;
 }
  </script>
 
  <body>
    <form name="form1" action="test.html" method="post" >
          客戶id: <input type="text" name="cid" value=""  id="cid"  ><br>
     客戶名稱<input type="text" name="cname" value=""  id="cname"  >
    <input type="button" name="ok" value="請選擇客戶" onclick="openWin();"/>
      </form>
</body>
 
</html>

子視窗:a2.html

 代碼如下 複製代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>a2.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
   <script language="JavaScript">
 function viewData(id,name){
  window.opener.setValues(id,name);
  window.opener = null;
  window.close();
 }
  </script>
  <body>
      <table border="1">
         <tr>
          <td>操作</td>
   <td>客戶id</td>
   <td>客戶名稱</td>
         </tr>
   
    <tr>
          <td><input type="button" value="選擇" id="ss" onclick="viewData('001','深圳華為')"></td>
   <td>001</td>
   <td>深圳華為</td>
         </tr>
    <tr>
          <td><input type="button" value="選擇"   onclick="viewData('002','用友軟體')"> </td>
   <td>002</td>
   <td>用友軟體</td>
         </tr>
      </table>
  </body>
 
 
 
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.