js擷取iframe和父級之間元素,方法、屬,擷取iframe的高度自適應iframe高度

來源:互聯網
上載者:User

標籤:引入   之間   offset   title   his   tle   調用   ie8   ==   

摘自:http://blog.csdn.net/kongjiea/article/details/388703991、在父頁面 擷取iframe子頁面的元素

(在同域的情況下 且在http://下測試,且最好在iframe onload載入完畢後 dosomething...)

js寫法a、同過contentWindow擷取

也有用contentDocument 擷取的 但是contentWindow 相容各個瀏覽器,可取得子視窗的 window 對象。
contentDocument Firefox 支援,> ie8 的ie支援。可取得子視窗的 document 對象。

擷取方法

[html] view plain copy  print?
  1. var frameWin=document.getElementById(‘iframe‘).contentWindow;    //window對象  
  2. var frameDoc=document.getElementById(‘iframeId‘).contentWindow.document  //document對象  
  3. var frameBody=document.getElementById(‘iframeId‘).contentWindow.document.body   //body對象  

還有iframe.contentDocument 方法 //但是ie 6,7不支援

 

 

 

b、通過frames[]數組擷取

(但是必須在ifram架構載入完畢後擷取,iframe1是iframe的name屬性)

 

[html] view plain copy  print?
  1. document.getElementById(‘iframId‘).onload=function(){  
  2.     var html= window.frames["name屬性"].document.getElementById(‘iframe中的元素的id‘).innerHTML;  
  3.     alert(html)  
  4. }  
  5.  如果要擷取iframe中的iframe  
  6. document.getElementById(‘iframId‘).onload=function(){  
  7.     var html= window.frames["name屬性"].frames["name屬性"].document.getElementById(‘iframe中的元素的id‘).innerHTML;  
  8.     alert(html)  
  9. }  
jq寫法:必須在iframe載入完畢以後才有效[html] view plain copy  print?
  1. a、$("#iframe的ID").contents().find("#iframe中的控制項ID").click();//jquery 方法1 必須在iframe載入完後才有效  
  2. b、$("#iframe中的控制項ID",document.frames("frame的name").document)//方法2 <span style="font-family: Arial, Helvetica, sans-serif;">必須在iframe載入完後才有效</span>  

 

 

=================================

2、在iframe中擷取父級頁面的id元素 

(在同域的情況下且在http://下測試,最好是 iframe記載完畢再dosomething)

js寫法:

擷取父級中的objid

 

[html] view plain copy  print?
  1. var obj=window.parent.document.getElementById(‘objId‘)  

window.top 方法可以擷取父級的父級的....最頂層的元素對象

jq寫法:[html] view plain copy  print?
  1. $(‘#父級視窗的objId‘, window.parent.document).css(‘height‘:‘height);  // window可省略不寫  
  2. 或者  
  3. $(window.parent.document).find("#objId").css(‘height‘:‘height);   // window可省略不寫  

===================================

3、父級表單訪問iframe中的屬性  

(經測試,在ie中最好用原生的onload事件,如果用jq的load把iframe載入完畢 有時候方法調用不到 多重新整理才有效果)

 

[html] view plain copy  print?
  1. a、 用contentWindow方法   
  2. document.getElementById(‘iframe1‘).onload=function(){  
  3.          this.contentWindow.run();  
  4.  }  
  5. b、用iframes[]框架組數組方法  
  6. document.getElementById(‘iframe1‘).onload=function(){  
  7.          frames["iframe1"].run();  
  8. }  

 

===================================

4、在iframe中訪問父級表單的方法和屬性 //window 可以不寫

 

[html] view plain copy  print?
  1. window.parent.attributeName;  // 訪問屬性attributeName是在父級視窗的屬性名稱  
  2. window.parent.Func();  // 訪問屬性Func()是在父級視窗的方法  

 

5、讓iframe自適應高度[html] view plain copy  print?
  1. $(‘#iframeId‘).load(function() { //方法1  
  2.     var iframeHeight = Math.min(iframe.contentWindow.window.document.documentElement.scrollHeight, iframe.contentWindow.window.document.body.scrollHeight);  
  3.     var h=$(this).contents().height();  
  4.     $(this).height(h+‘px‘);   
  5. });  
  6.   
  7. $(‘#iframeId‘).load(function() { //方法2  
  8.     var iframeHeight=$(this).contents().height();  
  9.     $(this).height(iframeHeight+‘px‘);   
  10. });  

6、iframe的onload的事件,主流瀏覽器都支援 iframe.onload=function....在ie下需要用attachEvent綁定事件 7、在iframe所引入的網址寫入防網路釣魚代碼if(window!=window.top){
window.top.location.href=window.location.href;
} 8、擷取iframe的高度iframe.contentWindow.document.body.offsetHeight;

js擷取iframe和父級之間元素,方法、屬,擷取iframe的高度自適應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.