iframe子窗口、父窗口相互調用、獲取內容

內容來自https://www.cnblogs.com/thiaoqueen/p/7277964.html,摘抄……

父頁面:

<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
<title>iframe(子)頁面內容</title>  
</head>  
  
<body>  
	<div id="iframeElementId">父頁面元素內容</div>  
</body>  
</html>

子頁面:

<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
<title>iframe(子)頁面內容</title>  
</head>  
  
<body>  
	<div id="iframeElementId">www.koyoz.com</div>  
</body>  
</html>

js示例:

<script type='text/javascript'>
	//1.在父頁面中(index.html)執行JS直接訪問子頁面(iframe.html)中的元素
	//Javascript寫法:
		document.getElementById('frameId').contentWindow.document.getElementById('iframeElementId').style.color='red';
	//jQuery寫法:
		$("#koyoz").contents().find("#test").css('color','red'); 
	//2.實用的示例1:在父窗口中操作,選中IFRAME中的所有單選鈕。
	//jQuery寫法:
		$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");
	//3.實用的示例2:在子窗口中操作,選中父窗口中的所有單選鈕。
	//jQuery寫法:
		$(window.parent.document).find("input:radio").attr("checked","true");
	//4.實用的示例3:父窗口想獲得IFrame中的Iframe
	//jQuery寫法:
		$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true");
	//5.實用的示例4:在子窗口中調用父窗口中的另一個子窗口的方法
	//jQuery寫法:
		parent.frames["Main"].Fun();
	
	//【其他方法】:
	//在iframe子頁面獲取父頁面元素
		$('#objId', parent.document);
	//在父頁面 獲取iframe子頁面的元素
		$("#objid",document.frames('iframename').document);
		$(document.getElementById('iframeId').contentWindow.document.body).html();
	//顯示iframe中body元素的內容。
		$("#testId", document.frames("iframename").document).html();
	//根據iframename取得其中ID爲"testId"元素
		$(window.frames["iframeName"].document).find("#testId").html();
</script>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章