標籤:引入 之間 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?
- var frameWin=document.getElementById(‘iframe‘).contentWindow; //window對象
- var frameDoc=document.getElementById(‘iframeId‘).contentWindow.document //document對象
- 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?
- document.getElementById(‘iframId‘).onload=function(){
- var html= window.frames["name屬性"].document.getElementById(‘iframe中的元素的id‘).innerHTML;
- alert(html)
- }
- 如果要擷取iframe中的iframe
- document.getElementById(‘iframId‘).onload=function(){
- var html= window.frames["name屬性"].frames["name屬性"].document.getElementById(‘iframe中的元素的id‘).innerHTML;
- alert(html)
- }
jq寫法:必須在iframe載入完畢以後才有效[html] view plain copy print?
- a、$("#iframe的ID").contents().find("#iframe中的控制項ID").click();//jquery 方法1 必須在iframe載入完後才有效
- 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?
- var obj=window.parent.document.getElementById(‘objId‘)
window.top 方法可以擷取父級的父級的....最頂層的元素對象
jq寫法:[html] view plain copy print?
- $(‘#父級視窗的objId‘, window.parent.document).css(‘height‘:‘height); // window可省略不寫
- 或者
- $(window.parent.document).find("#objId").css(‘height‘:‘height); // window可省略不寫
===================================
3、父級表單訪問iframe中的屬性
(經測試,在ie中最好用原生的onload事件,如果用jq的load把iframe載入完畢 有時候方法調用不到 多重新整理才有效果)
[html] view plain copy print?
- a、 用contentWindow方法
- document.getElementById(‘iframe1‘).onload=function(){
- this.contentWindow.run();
- }
- b、用iframes[]框架組數組方法
- document.getElementById(‘iframe1‘).onload=function(){
- frames["iframe1"].run();
- }
===================================
4、在iframe中訪問父級表單的方法和屬性 //window 可以不寫
[html] view plain copy print?
- window.parent.attributeName; // 訪問屬性attributeName是在父級視窗的屬性名稱
- window.parent.Func(); // 訪問屬性Func()是在父級視窗的方法
5、讓iframe自適應高度[html] view plain copy print?
- $(‘#iframeId‘).load(function() { //方法1
- var iframeHeight = Math.min(iframe.contentWindow.window.document.documentElement.scrollHeight, iframe.contentWindow.window.document.body.scrollHeight);
- var h=$(this).contents().height();
- $(this).height(h+‘px‘);
- });
-
- $(‘#iframeId‘).load(function() { //方法2
- var iframeHeight=$(this).contents().height();
- $(this).height(iframeHeight+‘px‘);
- });
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高度