window.parent與window.opener的區別 轉

來源:互聯網
上載者:User

1:   window.parent 是iframe頁面調用父頁面對象

舉例:
a.html

 

程式碼<html>
<head><title>父頁面</title></head>
<body>
<form name="form1" id="form1">

<input type="text" name="username" id="username"/>

</form>
<iframe src="b.html" width=100%></iframe>
</body>
</html>

如果我們需要在b.htm中要對a.htm中的username文字框賦值,就如很多上傳功能,上傳功能頁在Ifrmae中,上傳成功後把上傳後的路徑放入父頁面的文字框中

我們應該在b.html中寫

程式碼<script type="text/javascript">
var _parentWin = window.parent ;
_parentWin.form1.username.value = "xxxx" ;
</script>

2:   window.opener 是window.open 開啟的子頁面調用父頁面對象

a.html

程式碼<script type="text/javascript">
function openSubWin()
{
var _width = 300 ;
var _height = 200 ;
var _left = (screen.width - _width) / 2 ;
var _top = (screen.height - _height) / 2 ;
window.open("b.html",null,
"height=" + _height + ",width=" + _width + ",status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=" + _left + ",top=" + _top);
}
</script>
<input type="text" name="username" id="username"/>
<input type="button" value="彈出子頁面" onClick="openSubWin();">

b.html

程式碼<script type="text/javascript">
function UpdateParent()
{
var _parentWin = window.opener ;
_parentWin.form1.username.value = "xxxx" ;
}
</script>
<input type="button" name="button" id="button" value="更新首頁面的UserName內容" onClick="UpdateParent();"> 

聯繫我們

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