做過表單美化的人都知道,select 是最難搞的元素。瀏覽器對它的樣式控制極為有限,尤其在 IE 下,高度幾乎無法調整,四周永遠留著空隙;換到 Firefox 又呈現另一種樣子。想追求視覺一致,唯一的方法就是用其他元素「模擬」它。
我一開始找到了 jNice Form,這是一個專門美化表單的 jQuery 外掛。它的優點是不需要另外添加樣式或 id,就算之後不使用這個外掛,頁面結構也完全不必更動。
但表單美化裡,select 是最難模擬的一項,jNice 對它的處理也不算理想。於是我決定自己動手改造。
花了大約三個半小時,我改寫了原本的下拉選單模擬邏輯,解決了原版的幾個問題:
"#""#",若 select 位於頁面下方,選完會被跳回頁面頂端第一版上線後,我才注意到一個嚴重的問題:頁面載入完成後,所有被模擬的選項會短暫地全部顯示在頁面底部,造成畫面閃動。這不是修補能解決的架構問題,於是我重新寫了一份。
if ($.browser.msie) {
$(document).ready(function () {
// 對每一個 select 進行美化
$("select").each(function () {
select_name = $(this).attr("name");
width = $(this).width();
input_width = width - 17; // 預留給右側箭頭圖
offset = $(this).offset(); // 取得絕對座標
// 以 input + img 組出偽裝的下拉框
select_Html =
'<div style="width:' + width + 'px;position:absolute;' +
'left:' + offset.left + 'px;top:' + offset.top + 'px">' +
'<input type="text" style="width:' + input_width + 'px;" ' +
'autocomplete="off" readonly="true" ' +
'id="input_' + select_name + '" class="select_input" />' +
'<img id="img_' + select_name + '" name="' + select_name + '" ' +
'class="select_img" src="s.gif" width="17" height="21" />' +
'</div>';
$("body").append(select_Html);
$(this).css({ visibility: "hidden" }); // 原生的藏起來但保留佔位
// 組出選項清單
option_num = $("option", $(this)).length;
option_height = option_num * 21 > 200 ? 200 : option_num * 21;
option_width = width;
option_left = offset.left;
option_top = offset.top + 21;
option_Html =
'<div class="select_option_div" id="option_' + select_name + '" ' +
'style="height:' + option_height + 'px;width:' + option_width + 'px;' +
'position:absolute;top:' + option_top + 'px;left:' + option_left +
'px;overflow:auto">';
$(this).find("option").each(function () {
option_Html += '<div class="select_option">' + $(this).text() + '</div>';
});
option_Html += '</div>';
$("body").append(option_Html);
$("#option_" + select_name).hide();
img_id = "#img_" + select_name;
input_id = "#input_" + select_name;
// 點箭頭:切換展開/收合
$(img_id).click(function () {
dest_id = "#option_" + $(this).attr("name");
state = $(dest_id).css("display");
$(".select_option_div").hide(); // 先全部關掉
if (state == "none") { $(dest_id).show(); }
else { $(dest_id).hide(); }
});
// 點輸入框:同樣可以展開
$(input_id).click(function () {
dest_id = "#option_" + $(this).attr("id").substr(6);
state = $(dest_id).css("display");
$(".select_option_div").hide();
if (state == "none") { $(dest_id).show(); }
else { $(dest_id).hide(); }
});
// 還原原生選單的預設選中項
obj = document.getElementById(select_name);
input_id = "#input_" + select_name;
$(input_id).val(obj.options[obj.selectedIndex].text);
});
// 點選某個選項
$(".select_option").click(function () {
parent_id = $(this).parent().attr("id").substr(7);
input_id = "#input_" + parent_id;
$(input_id).val($(this).text()); // 顯示在偽裝的輸入框
obj = document.getElementById(parent_id);
obj.options[obj.selectedIndex].text = $(this).text();
$("#option_" + parent_id).hide();
});
// 選項的滑鼠移入移出效果
$(".select_option").hover(
function () { $(this).addClass("select_highlight"); },
function () { $(this).removeClass("select_highlight"); }
);
});
// 點擊頁面其他處時收起所有選項
$(document).click(function (evt) {
var e = evt || window.event;
var target = e.target || e.srcElement;
var cls = target.className;
if (cls == "select_img" || cls == "select_input" || cls == "select_option") {
return false;
} else {
$(".select_option_div").hide();
}
});
}
用 offset() 定位而非 flow 排版:因為要把模擬層疊在原 select 位置上,取得絕對座標是唯一可靠的方式。
原生 select 用 visibility: hidden 而非 display: none:前者保留佔位空間,表單提交時它也仍然是正常的表單欄位,後端程式完全不必修改。
用事件委派思路處理「點空白處收合」:在 document 上攔截點擊,若目標不是自己產生的元素就全部收起。
這個方案有明顯的限制:不支持鍵盤操作,對螢幕閱讀器也不友善。以今日的標準來看,更值得推薦的做法是保留原生 select,只美化它的容器與箭頭,或採用已內建無障礙支援的元件庫(如 jQuery UI Selectmenu、Select2)。自製模擬始終是在與瀏覽器預設行為對抗,維護成本遠高於表面看起來的樣子。
原始展示頁面:
https://www.live-my-life-with-yuyi.com/lab/jquery/form_select/