在 EasyUI 的 Layout 框架中,內容頁通常各自被包在一個 iframe 裡。這帶來一個惱人的問題:頁內彈出的視窗會被限制在自己那個 iframe 的範圍內,永遠蓋不過外層的 Layout。
思路很直接——不要在 iframe 內部開視窗,而是請最外層的 parent 代開,並把 iframe 的內容塞進去。
function openTopWindow(url, title, width, height) {
title = title == undefined ? ' ' : title;
width = width == undefined ? 800 : width;
height = height == undefined ? 300 : height;
if (url != undefined) {
var content = '<iframe name="eui-open-page" scrolling="auto" frameborder="0" ' +
'src="' + url + '" style="width:100%;height:100%;"></iframe>';
parent.$('#openWindow').window({
title: title,
width: width,
height: height,
content: content,
modal: true,
animate: true,
minimizable: false
});
}
}
注意 parent.$('#openWindow') 這個寫法:它操作的是最外層頁面的 jQuery 物件,因此彈窗自然被掛在 Layout 之外的最高層。
接著在 Layout 主頁面放一個空的 div 當作彈窗的落腳處:
<!-- Open Window Begin --> <div id="openWindow"></div> <!-- Open Window End -->
這個函式適合放在 Layout 主頁面的共用 JS 中,讓各個 iframe 內容頁都能直接呼叫。它解決了「彈窗被裁切」的問題,也讓 modal 遮罩能正確覆蓋整個工作區。
但要留意:由於彈窗內容是另一個 iframe,父子之間若要互通資料,仍需注意同源限制與呼叫時機。建議在彈窗 iframe 的頁面 load 完成後,再由父層透過 contentWindow 進行操作,避免取到尚未初始化的物件。