js父頁面和子頁面之間傳值,js頁面傳值

來源:互聯網
上載者:User

js父頁面和子頁面之間傳值,js頁面傳值

今天和朋友一塊討論,如何通過js在父頁面和子頁面之間傳值的問題,總結如下:

需求描述:父頁面有多個子頁面,實現在父頁面點擊子頁面,傳值到子頁面。

看著很簡單,試了好久,主要糾結在如何擷取iframe,剛開始用document.getElementById('iframe的ID'),後來查資料才發現這種方法只是去取出了一個OBJECT,不能操作它的函數方法,後來改成window.iframes["iframe的name"],這樣就可以擷取子頁面的整個DOM,可以操作它的函數。

1、父頁面代碼

<html><head><title></title><script type="text/javascript" >function callChild(){var child_1 =window.frames["child"];var str = "ddddd";child_1.window.testJs(str);}function aa(str){alert(str);}</script></head><body><iframe name="child" src="children.html" ></iframe><a onclick="callChild()">call children</a></body></html>

2、子頁面代碼

<html><head><title></title><script type="text/javascript">function testJs(str){alert(str);}function callParent(){var str = "vdsss";parent.aa(str)}</script></head><body><a onclick="callParent()">call parent</a></body></html>




js子頁面向父頁面傳值問題

父視窗添加
<input type="hidden" id="s"/>

子視窗
window.parent.docuent.getElementById("s").value=s;

父視窗獲得傳的值
var str = document.getElementById("s").value;
alert(str);
 
js 父頁面與子頁面相互傳值

子頁面有值時,直接調用父頁面的函數,把子頁面的值作為參數傳進去就可以了
window.opener.父頁面函數(子頁面值);
 

聯繫我們

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