JS擷取/設定iframe內對象元素、文檔的幾種方法

來源:互聯網
上載者:User

標籤:

1、IE專用(通過frames索引形象定位):

document.frames[i].document.getElementById(‘元素的ID‘);

2、IE專用(通過iframe名稱形象定位):

document.frames[‘iframe的name‘].document.getElementById(‘元素的ID‘);

以上方法,不僅對iframe適用,對frameset裡的frame也同樣適用。IE雖然擅於自定標準,但不得不說它很多的設計還是比較體現人性化的。比如這個,它在同樣支援下面的標準路徑之外,提供了一個簡潔且形象化的寫法。

3、通用方法:

document.getElementById(‘iframe的ID‘).contentWindow.document.getElementById(‘元素的ID‘)

注意要加上contentWindow,往往出現問題都是因為這個容易被忽略,它代表frameiframe內部的視窗對象

 

 

 

 

JS擷取iframe文檔內容

<script type="text/javascript"> function getIframeContent(){  //擷取iframe中文檔內容var doc;if (document.all){ // IE doc = document.frames["MyIFrame"].document; }else{ // 標準doc = document.getElementById("MyIFrame").contentDocument; }return doc.body.innerHTML;} </script>

注意:上面的 .contentDocument 相當於 .contentWindow.document !

 

 

延伸閱讀:

實用架構(iframe)代碼

JS擷取/設定iframe內對象元素、文檔的幾種方法

聯繫我們

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