獲取iframe中的document元素有一下集中方法:
1、getElementById()方法和contentWindow屬性:
window.onload=function(){ /*必須等待頁面加載結束后*/
document.getElementById("iframe的ID").contentWindow.document.getElementById("元素的ID")
document.getElementById("iframe的ID").contentDocument.getElementById("元素的ID") }
注意:上面的 .contentDocument (不能用於IE,因為IE不存在這個屬性)相當於 .contentWindow.document !
2、window.對象的frames屬性
window.onload=function(){ /*必須等待頁面加載結束后*/ window.frames["索引或者iframe的name屬性值"].document.getElementById("元素的ID") }
window.frames屬性引用的是類數組對象,並可以通過數字或窗體名進行索引。
window.frames[0] //窗口的第一個子窗體
window.frames["iframe的name屬性值"] //window.frames["f1"]或者window.frames.f1
注意:frames[]數組里的元素是Window對象,而不是<iframe>元素
3、iframe的name屬性值
window.onload=function(){ /*必須等待頁面加載結束后*/ iframe的name屬性值".document.getElementById("元素的ID") /*窗體的名字是指窗體中的window對象*/
}
注意:可以通過窗體的名字“f1”來代替frames.f1.
補充:
獲取iframe的窗體: document.getElementById("iframe的ID").contentWindow = window.frames["索引或者iframe的name屬性值"] = iframe的name的屬性值
