照理說有 z-index 就能決定層級,但在 IE6 裡,<select> 是原生視窗控制項(windowed control),層級永遠壓在 HTML 元素之上。於是彈跳視窗、遮罩、浮動工具列一遇到下拉選單就破功。這是老專案維護時的固定關卡。
bgiframe 的原理是在被遮住的元素底下插入一個 <iframe>,由於 iframe 在 IE6 中也屬於視窗層級,於是形成「iframe 蓋住 select,div 蓋住 iframe」的三層結構。
<script type="text/javascript" src="resources/js/plugins/jquery/jquery.bgiframe.min.js"></script>
$('#div_bksq').bgiframe();
要在元素顯示之後呼叫,若一開始是 display:none,記得在 open 的時機再執行一次,否則 iframe 尺寸會是 0。
<!-- 新增、申請與修改布控頁面開始 -->
<div class="gjcx_pop" style="display: none; background: white; height: 480px; margin-top: -240px;" id="div_bksq">
<input type="text" style="display: none" id="txt_isViewOrUpdate" />
<div class="gjcx_pop_tit" style="cursor: move;" id="div_bksq_tit">
<span>
<img src="resources/themes/default/images/vts/sourch_ico.gif" />
<font class="sxcx_titText">
<input type="text" id="txt_bt" style="border:0px;width:60px;
font: normal bold 13px '宋體';color: #00517e;text-decoration: none;
float:left;background: url('resources/themes/default/images/vts/alert_tit.jpg') repeat-x left top" />
</font>
</span>
<img src="resources/themes/default/images/vts/alert_close.jpg" id="img_gb"
style="cursor: pointer;" title="關閉" />
</div>
<div class="qvyu" id="qyTree">
<div class="qvyu_n">
<!-- 樹狀內容 -->
</div>
</div>
</div>
select 設為 visibility:hidden,關閉時還原。做法粗糙但零依賴。div + ul 模擬下拉選單,徹底擺脫原生控制項限制,也日後跨瀏覽器都一致。範例中大量使用行內 style 與 <font> 標籤,維護性不佳。若這個區塊還會長期存在,建議把樣式搬到 CSS 檔,並留意 margin-top: -240px 這類寫死值的定位方式在不同視窗高度下容易偏移,改用 position:fixed 搭配 top:50%;transform:translateY(-50%) 會穩定許多。