在頁面嵌套 iframe 的場景中,經常需要父子頁面之間互相訪問 DOM 元素。本文整理了几種常見的獲取方式。
$.('#objld', parent.document);
$("#objid", document.iframes('iframe').document)
或者使用 contentWindow 的方式:
$(document.getElementById('iframeId').contentWindow.document.body).html()
$("#testId", document.frames("iframename").document).html()
需要留意的是,父子頁面互相訪問 DOM 時必須處於同源環境,否則會因跨域安全策略而無法訪問。