js如何在iframe父頁面和子頁面中操做dom

jquery操做包含iframe的頁面中元素的幾種方法:html

//
//在iframe子頁面獲取父頁面元素
$('#objId', parent.document);
$(window.parent.document).find("#objId");

//在父頁面獲取iframe子頁面的元素
$("#objid",document.frames('iframename').document);
$("#objid",window.frames["iframeName"].document);
$("#objid",document.getElementById('iframeId').contentWindow.document);
$(window.frames["iframeName"].document).find("#objid");
$("#iframeId").contents().find("#objid");

//例子
//顯示iframe中body元素的內容
$(document.getElementById('iframeId').contentWindow.document.body).html();
//例子
//顯示iframe中id爲testId的元素的內容
$("#testId", document.frames("iframename").document).html();
$(window.frames["iframeName"].document).find("#testId").html()
//例子
//在父窗口中操做,選中IFRAME中的全部單選鈕
$(window.frames["iframeName"].document).find("input:radio").attr("checked","true");
//在IFRAME子窗口中操做,選中父窗口中的全部單選鈕
$(window.parent.document).find("input:radio").attr("checked","true");

//再進一步
//父窗口想得到IFrame中的Iframe,就再加一個frames子級就好了
$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio")

注意:因爲安全問題,協議規定框架內的頁面是不能跨域的!jquery

相關文章
相關標籤/搜索