select控件的优先级非常高,许多css属性对它没有作用,精心设计的页面因为一个突兀的原生select控件而减分是大家都头痛的事,
下面的方法是通过JS替换页面内原有的select,
我们希望这个JS是非侵入式的,只要引用了select.js这个js文件的页面,就自动把页面里原有的select控件替换接管了。
先初步地实现selct控件的替换,和基本的交互(暂不考虑接管select的onchange事件等问题)。在下面的实现方法里并没有把原有的select去掉,只是隐藏了起来,所以如果select是在表单内,表单仍然能够正常提交。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<style>
body,
table,
input,
textarea,
select{
margin: 0;
font-size: 12px;
line-height:1.5;
font-family: Tahoma, SimSun, sans-serif;
}
.zSelect {
display:inline-block;
*zoom: 1;
*display: inline;
position:relative;
height:20px;
vertical-align:middle;
}
.zSelect .inputText {
line-height: 17px;
font-size:12px;
background: #f7fafc;
padding: 1px 17px 0 1px;
border: 1px solid #68a;
vertical-align: top;
cursor:default;
height: 17px;
margin:0;
}
.zSelect .arrowimg {
display:inline-block;
*zoom: 1;
*display: inline;
position:relative;
cursor:pointer;
width:18px;
height:20px;
left:-18px;
margin-right:-18px;
vertical-align: top;
outline:none;
background: url(http://www.wangzhaohui.com/wp-content/uploads/2009/06/arrow.gif);
}
.zSelect .arrowimg:hover {
background: url(http://www.wangzhaohui.com/wp-content/uploads/2009/06/arrow_over.gif);
}
.optgroup {
position:absolute;
z-index:666;
left:0;
top:19px;
color: #369;
}
.optgroup p{ margin:0;}
.optgroup div.optionsDiv{
padding:1px;
overflow: auto;
overflow-x: hidden;
max-height:300px;
color: #369;
border: 1px solid #678;
background: #f7fafc;
width:auto;
z-index:888;
filter: Alpha(Opacity=90); opacity: 0.9;
}
.optgroup a, .optgroup a:visited{
font-size:12px;
text-decoration:none;
cursor:default;
display:block;
color: #369;
white-space: nowrap;
padding:1px 3px 2px 6px;
_padding:0 3px 0 6px;
height:18px;
min-width:2em;
}
.optgroup a:hover,.optgroup a.selected:hover{
color: #dff;
text-decoration:none;
background:#38d;
}
.optgroup a.selected,.optgroup a:focus{
color: #eff;
text-decoration:none;
background:#49e;
}
</style>
<script>
function replaceSelects() {
selects = document.getElementsByTagName('select');
for(var i=0; i < selects.length; i++) {
var selectWidth=selects.clientWidth;
var selectArea = document.createElement('span');
var textInput = document.createElement('input');
var button = document.createElement('a');
selectArea.id = "mySelect"+i;
selectArea.className = "zSelect";
textInput.type = "text";
textInput.className = "inputText";
textInput.readOnly=true;
textInput.style.width=selectWidth+"px";
textInput.id = "mySelectText"+i;
textInput.value = selects.options[0].text;
button.className = "arrowimg";
button.href="javascript:showOptions("+i+")";
button.hideFocus=true;
selectArea.appendChild(textInput);
selectArea.appendChild(button);
selects.style.display='none';
selects.parentNode.insertBefore(selectArea, selects);
var optgroup = document.createElement('div');
optgroup.className = "optgroup";
optgroup.style.width=selectWidth+20+"px";
optgroup.style.display = "none";
optgroup.id = "optgroup"+i;
var optionsDiv = document.createElement('div');
optionsDiv.className = "optionsDiv";
optionsDiv.id = "optionsDiv"+i;
optgroup.appendChild(optionsDiv);
if(selects.id=="")selects.id="select"+i;
selectArea.appendChild(optgroup);
for(var j=0; j < selects.options.length; j++) {
var optionHolder = document.createElement('p');
var optionLink = document.createElement('a');
var optionTxt = document.createTextNode(selects.options[j].text);
optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects.id+"',"+j+","+i+");";
optionLink.appendChild(optionTxt);
optionHolder.appendChild(optionLink);
optionsDiv.appendChild(optionHolder);
if(selects.options[j].selected){selectMe(selects.id,j,i);}
}
}
}
function showOptions(g) {
var elem = document.getElementById("optgroup"+g);
elem.style.display=elem.style.display=='none'?'block':'none';
}
function selectMe(selectFieldId,linkNo,selectNo) {
optionLinks = document.getElementById("optionsDiv"+selectNo).getElementsByTagName("a");
for(var k = 0; k < optionLinks.length; k++) {
if(k==linkNo) {
optionLinks[k].className = "selected";
}
else {
optionLinks[k].className = "";
}
}
selectField = document.getElementById(selectFieldId);
for(var k = 0; k < selectField.options.length; k++) {
if(k==linkNo) {
selectField.options[k].selected = "selected";
}
else {
selectField.options[k].selected = "";
}
}
var newText = selectField.options[linkNo].text;
document.getElementById("mySelectText"+selectNo).value=newText;
}
window.onload=replaceSelects;
</script>
<div>当前站点:
<select name="select">
<option value="123123">政府门户类演示站</option>
<option value="456456">新闻门户类演示站</option>
<option value="789789">企业形象类演示站</option>
</select>
<<
当前站点:
<select name="select2">
<option value="123123">政府门户类演示站</option>
<option value="456456">新闻门户类演示站</option>
<option value="789789">企业形象类演示站</option>
</select>
<<
</div>
OK,在ff3下测试通过,在ie下存在层的定位问题,当弹出下拉列表时需要对层的z-index作调整,在这就不展示了。
如果这个select控件仅在前台作小量的应用,那么适当地添加一些对键盘,鼠标的响应,就差不多可以了。
select控件实现的思考过程和进一步优化请看我的另一个帖子
《分享我们的select控件设计过程 》