1. 程式人生 > >iframe子頁面獲取父頁面元素的方法

iframe子頁面獲取父頁面元素的方法

在iframe子頁面獲取父頁面元素 

 $.('#objld', parent.document);

在父頁面獲取iframe子頁面的元素  

$("#objid",document.iframes('iframe').document)或$(document.getElementById('iframeId').contentWindow.document.body).html()

顯示iframe中body元素的內容  

 $("#testId", document.frames("iframename").document).html()  

(1)父頁面獲取iframe中的元素值:

Js程式碼 

1.//根據iframe的id獲取物件  

2.var i1 = window.frames['iframeId'];  

3.//var iframe =window.frames[0];也可以  

4.//獲取iframe中的元素值  

5.var val=i1.document.getElementById("t1").value  //根據iframe的id獲取物件

var i1 = window.frames['iframeId'];

//var iframe =window.frames[0];也可以 //獲取iframe中的元素值

var val=i1.document.getElementById("t1").value

(2)在iframe中獲取父頁面中的元素值:

Js程式碼 

1.var val = parent.document.getElementById("txt1");  

var val = parent.document.getElementById("txt1");

(3)a包含2個iframe分別為b,c,現在從b中獲取c中的元素的值:

Js程式碼 

1.var i1 = parent.window.frames['iframeId'];  

2.var val = i1.document.getElementById("text1").value; 

var i1 = parent.window.frames['iframeId'];

var val = i1.document.getElementById("text1").value;