取得 iframe 的 document 物件,主要目的是為了對其進行 DOM 操作。做法相當簡潔:
$(function() { var result = $('#myframe').prop('contentWindow').document; console.log(result); });
這樣輸出來的結果就是一個 document 物件。透過 prop('contentWindow') 取得 iframe 的內容視窗,再取用其 document,就能進一步選取並操作 iframe 內部的元素了。