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

首頁 > 語言 > JavaScript > 正文

js操作iframe的一些方法介紹

2024-05-06 15:48:17
字體:
來源:轉載
供稿:網友
本篇文章介紹了js操作iframe的一些方法的實例代碼與步驟,有需要的朋友可以參考一下

1. 獲得iframe的window對象
存在跨域訪問限制。

chrome:iframeElement. contentWindow
firefox: iframeElement.contentWindow
ie6:iframeElement.contentWindow

文章Iframes, onload, and document.domain中說“he iframe element object has a property called contentDocument that contains the iframe's document object, so you can use the parentWindow property to retrieve the window object.”意思就是一些瀏覽器可以通過iframeElement.contentDocument.parentWindow獲得iframe的window對象。但經過測試firefox、chrome的element.contentDocument對象沒有parentWindow屬性。

(javascript)

復制代碼 代碼如下:


function getIframeWindow(element){
return element.contentWindow;
//return element.contentWindow || element.contentDocument.parentWindow;
}

2. 獲得iframe的document對象
存在跨域訪問限制。

chrome:iframeElement.contentDocument
firefox:iframeElement.contentDocument
ie:element.contentWindow.document
備注:ie沒有iframeElement.contentDocument屬性。

(javascript)

復制代碼 代碼如下:


var getIframeDocument = function(element) {
return element.contentDocument || element.contentWindow.document;
};

3. iframe中獲得父頁面的window對象
存在跨域訪問限制。

父頁面:window.parent
頂層頁面:window.top
適用于所有瀏覽器

4. 獲得iframe在父頁面中的html標簽
存在跨域訪問限制。

window.frameElement(類型:HTMLElement),適用于所有瀏覽器

5. iframe的onload事件
非ie瀏覽器都提供了onload事件。例如下面代碼在ie中是不會有彈出框的。

(javascript)

復制代碼 代碼如下:


var ifr = document.createElement('iframe');
ifr.src = 'http://www.survivalescaperooms.com/index.php';
ifr.onload = function() {
alert('loaded');
};
document.body.appendChild(ifr);


但是ie卻又似乎提供了onload事件,下面兩種方法都會觸發onload

方法一:

復制代碼 代碼如下:


<iframe src="http://www.survivalescaperooms.com/index.php"></iframe>


方法二:
//只有ie才支持為createElement傳遞這樣的參數

復制代碼 代碼如下:


var ifr = document.createElement('<iframe src="http://www.survivalescaperooms.com/index.php"></iframe>');
document.body.appendChild(ifr);


由于iframe元素包含于父級頁面中,因此以上方法均不存在跨域問題。

實際上IE提供了onload事件,但必須使用attachEvent進行綁定。

復制代碼 代碼如下:


var ifr = document.createElement('iframe');
ifr.src = 'http://b.vevb.com/b.php';
if (ifr.attachEvent) {
ifr.attachEvent('onload', function(){ alert('loaded'); });
} else {
ifr.onload = function() { alert('loaded'); };
}
document.body.appendChild(ifr);

6. frames
window.frames可以取到頁面中的幀(iframe、frame等),需要注意的是取到的是window對象,而不是HTMLElement。

復制代碼 代碼如下:


var ifr1 = document.getElementById('ifr1');
var ifr1win = window.frames[0];
ifr1win.frameElement === ifr1; // true
ifr1win === ifr1; // false

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

圖片精選

主站蜘蛛池模板: 萨嘎县| 西乌| 什邡市| 贡觉县| 柯坪县| 余姚市| 秭归县| 新源县| 台湾省| 海城市| 梓潼县| 东明县| 尤溪县| 忻州市| 和平县| 广平县| 威远县| 德化县| 保康县| 揭阳市| 肃南| 娱乐| 永嘉县| 陵水| 二连浩特市| 建瓯市| 孟津县| 松溪县| 岳普湖县| 宁陕县| 西华县| 三江| 新郑市| 泉州市| 台前县| 怀来县| 怀远县| 焦作市| 舒兰市| 右玉县| 财经|