var win = $('#ifr')[0].contentWindow;
var win = document.getElementById("ifr").contentWindow;
兩者等價——$('#ifr')[0] 取到的就是原生 DOM 節點,後面接 contentWindow 自然相同。
var ifr1 = document.getElementById("ifr");
var ifr2 = window.frames[0];
// ifr2.frameElement 等同 ifr1
也就是說,window.frames[n] 直接給你視窗物件,而 frameElement 則是反過來從視窗找回對應的 iframe 元素,兩者是互逆操作。
el.contentDocument || el.contentWindow.document,可同時覆蓋有無 contentDocument 的瀏覽器。load 事件再操作。postMessage 通訊。window.frames 的索引包含同一頁面內所有 frame/iframe,若頁面有多個 iframe,用 id 定位比用索引可靠。