日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iframe中父子窗口的调用

發布時間:2024/4/13 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iframe中父子窗口的调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、iframe標簽詳解

<iframe src="1.html" frameborder="0" id="child"></iframe>

(1)操作子窗口:frames['child'].contentDocument || frames['child'].document

1.首先獲取iframe節點:

var myIframe = document.getElementById('child'); 或 var myIframe = frames['child']; 或 var myIframe = window.frames['child']; 或 var myIframe = child;

window對象的frames屬性返回一個類似數組的對象,成員是所有子窗口的window對象。比如,frames[0]返回第一個子窗口。如果iframe設置了idname,那么屬性值會自動成為全局變量,并且可以通過window.frames屬性引用,返回子窗口的window對象。

window.frames['child'] === frames['child'] === child === document.getElementById('child');

2.然后獲取iframe包含的document對象:

IE瀏覽器:

var childDocument = myIframe.document;

其他瀏覽器:

var childDocument = myIframe.contentWindow.document; 或 var childDocument = myIframe.contentDocument;

contentWindow屬性獲得iframe節點包含的window對象,
contentDocument屬性獲得包含的document對象,等價于contentWindow.document

注意:iframe元素遵守同源政策,只有當父頁面與框架頁面來自同一個域名,兩者之間才可以用腳本通信,否則只有使用window.postMessage方法。

(2)操作父窗口:iframe.parent

iframe窗口內部,使用window.parent引用父窗口。如果當前頁面沒有父窗口,則window.parent屬性返回自身。因此,可以通過window.parent是否等于window.self,判斷當前窗口是否為iframe窗口。

if (window.parent !== window.self) {//當前窗口是子窗口var parentDocument = window.parent.document; }

二、iframe中父子窗口操作實例

父頁面:

<body> <div id="myList"> </div> <iframe src="1.html" frameborder="0" id="child"></iframe> </body>

子頁面:

<body> <div id="content"> 這是子頁面 </div> </body>

父頁面調取子頁面內容:

frames['child'].onload = function () {var childDocument = this.contentDocument || this.document; }

子頁面調取父頁面內容:

if (window.parent !== window.self) {var parentDocument = window.parent.document; }

總結

以上是生活随笔為你收集整理的iframe中父子窗口的调用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。