// 取值
var val = window.parent.document.getElementById("elementId").value;
// 取值 (jQuery)
var val = $(window.parent.document).find("#elementId").val();
// 設值
window.parent.document.getElementById("elementId").value = "新值";
// 設值 (jQuery)
$(window.parent.document).find("#elementId").val("新值");
// 呼叫父視窗函式
window.parent.parentFunction();
// 或
parent.parentFunction();
// 取值 (原生)
var iframe = document.getElementById("iframeId");
var val = iframe.contentWindow.document.getElementById("elementId").value;
// 取值 (jQuery)
var val = $("#iframeId").contents().find("#elementId").val();
// 設值
iframe.contentWindow.document.getElementById("elementId").value = "新值";
// 設值 (jQuery)
$("#iframeId").contents().find("#elementId").val("新值");
// 呼叫 iframe 內函式
document.getElementById("iframeId").contentWindow.childFunction();
// 或
window.frames["iframeName"].childFunction();
// 取得 iframe 的 document 物件 (相容 IE/FF/Chrome)
function getIframeDoc(iframeEl) {
if (iframeEl.contentDocument) {
return iframeEl.contentDocument; // W3C 標準 (FF, Chrome, IE9+)
} else if (iframeEl.contentWindow) {
return iframeEl.contentWindow.document; // IE8-
} else if (iframeEl.document) {
return iframeEl.document; // 極舊 IE
}
return null;
}
// 父視窗 → iframe 通用取值
function getIframeValue(iframeId, elementId) {
var iframe = document.getElementById(iframeId);
var doc = getIframeDoc(iframe);
return doc ? doc.getElementById(elementId).value : null;
}
// 父視窗 → iframe 通用設值
function setIframeValue(iframeId, elementId, value) {
var iframe = document.getElementById(iframeId);
var doc = getIframeDoc(iframe);
if (doc) doc.getElementById(elementId).value = value;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function setChildValue() {
// jQuery 方式
$('#childFrame').contents().find("#bbb").val("父視窗設定的值");
}
function parentFunc() {
alert("父視窗函式被子視窗呼叫!");
}
</script>
</head>
<body>
<div id="maindiv">父視窗內容</div>
<iframe id="childFrame" name="childFrame" src="b.html" width="600" height="400"></iframe>
<br>
<button onclick="setChildValue()">給子視窗輸入框賦值</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function callParent() {
// 呼叫父視窗函式
window.parent.parentFunc();
// 設定父視窗元素
$(window.parent.document).find("#maindiv").html("子視窗傳來的值");
}
</script>
</head>
<body>
<form name="cform">
<input type="text" name="b" id="bbb" />
<button type="button" onclick="callParent()">呼叫父視窗函式並賦值</button>
</form>
</body>
</html>
// 父視窗選取 iframe 所有文字輸入框
var $inputs = $(window.frames["iframeSon"].document).find(":text");
// iframe 選取父視窗所有文字輸入框
var $inputs = $(window.parent.document).find(":text");
// iframe 選取父視窗所有 radio 並全選
$(window.parent.document).find("input[type='radio']").prop("checked", true);
| 條件 | 結果 |
|---|---|
| 同源(協定+域名+埠號相同) | ✅ 可直接存取 DOM、呼叫函式 |
| 跨域 | ❌ 瀏覽器阻擋存取 contentWindow.document,拋出 SecurityError |
| 跨域但需通訊 | 使用 window.postMessage() |
// 父視窗
window.addEventListener("message", function(e) {
if (e.origin !== "https://child.example.com") return; // 驗證來源
if (e.data.action === "setValue") {
document.getElementById(e.data.id).value = e.data.value;
}
});
// 子視窗 (不同域名)
function notifyParent(id, value) {
window.parent.postMessage({
action: "setValue",
id: id,
value: value
}, "https://parent.example.com"); // 指定目標源
}
iframe.onload 後執行parent.$contentWindow vs contentDocument 混淆:前者是 Window 物件,後者是 Document 物件window.parent.parent 逐層向上,window.top 直接到頂層避免使用 iframe 傳遞資料。優先考慮: