功能需求
1、调用要方便,做好之后应该像这样
function loadSelect(selectobj){
//传入一个select对象就能将他的样式美化
}
2、不改变原有表单项,表单的页面代码不去破坏
%26lt;form name="f" onsubmit="getResult();"%26gt;
%26lt;fieldset%26gt;
%26lt;legend%26gt;用户注册%26lt;/legend%26gt;
%26lt;div%26gt;
%26lt;label for="username"%26gt;帐号%26lt;/label%26gt;
%26lt;input type="text" id="username" name="username" /%26gt;
%26lt;/div%26gt;
%26lt;div%26gt;
%26lt;label for="pwd"%26gt;密码%26lt;/label%26gt;
%26lt;input type="password" name="pwd" id="pwd" /%26gt;
%26lt;/div%26gt;
%26lt;div%26gt;
%26lt;label for="province"%26gt;省份%26lt;/label%26gt;
%26lt;select id="province" name="province"%26gt;
%26lt;option value="10"%26gt;江西%26lt;/option%26gt;
%26lt;option value="11"%26gt;福建%26lt;/option%26gt;
%26lt;option value="12"%26gt;广东%26lt;/option%26gt;
%26lt;option value="13"%26gt;浙江%26lt;/option%26gt;
%26lt;/select%26gt;
%26lt;/div%26gt;
%26lt;/fieldset%26gt;
%26lt;input type="submit" value="提交" name="btnSub" /%26gt;
%26lt;/form%26gt;
实现思路
第一步将表单中的select隐藏起来。
为什么?很简单,因为这家伙太顽固了,用css根本搞不出来你想要的。所以把它杀掉。
第二步用脚本找到select标签在网页上的绝对位置。
我们在那个位置上用DIV标签做个假的、好看点的来当他的替身。
第三步用脚本把select标签中的值读出来。
虽然藏起来了,但它里边的options我们还有用呢,统统取过来。
第四步当用户点击select标签的替身,也就是div的时候。我们再用一个div浮在上一个div的下边,这个就是options的替身了。
大致上就是这样了,接下来我们一步一步去实现它!
预备工作
1、想好你要把select美化成什么样子,并预备好相应的图片。我预备了两张小图,就是下拉箭头1和下拉箭头2,1是默认样式,2是鼠标移过来的样式。
2、写好一个普通的表单递交页面,比如下边这个。注重我给select定义了基本的CSS样式、在头部添加了调用js文件的代码、在body中添加了调用函数的脚本。
编写javascript
%26lt;script type="text/javascript" src="select.js"%26gt;%26lt;/script%26gt;
新建一个js文件并保存为select.js,剩下的工作我们全部在这个js中去完成。
函数名loadSelect(obj);
参数select对象
相关函数
//取标签的绝对位置
{
var t = e.offsetTop;
var l = e.offsetLeft;
var w = e.offsetWidth;
var h = e.offsetHeight-2;
while(e=e.offsetParent)
{
t+=e.offsetTop;
l+=e.offsetLeft;
}
return {
top : t,
left : l,
width : w,
height : h
}
}
第一步把select的绝对位置记录下来。一会替身上来就知道应该站那里了。
var offset=Offset(obj);
//这里解释一下Offset是一个函数,用来获取对象的绝对位置。写在loadSelect()函数外边的。他有四个属性分别是top/left/width/height。
第二步将select隐藏。
obj.style.display="none";
第三步虚拟一个div出来代替select
var iDiv = document.createElement("div");
iDiv.id="selectof" + obj.name;
iDiv.style.position = "absolute";
iDiv.style.width=offset.width + "px";
iDiv.style.height=offset.height + "px";
iDiv.style.top=offset.top + "px";
iDiv.style.left=offset.left + "px";
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
iDiv.style.border="1px solid #3366ff";
iDiv.style.fontSize="12px";
iDiv.style.lineHeight=offset.height + "px";
iDiv.style.textIndent="4px";
document.body.appendChild(iDiv);
第四步把原始select没人选中的值给它。
iDiv.innerHTML=obj.options[obj.selectedIndex].innerHTML;
第五步为替身添加鼠标移过样式。
iDiv.onmouseover=function(){//鼠标移到
iDiv.style.background="url(icon_select_focus.gif) no-repeat right 4px";
}
iDiv.onmouseout=function(){//鼠标移走
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
}
第六步添加要害的鼠标点击事件。
iDiv.onclick=function(){//鼠标点击
if (document.getElementById("selectchild" + obj.name)){
//判定是否创建过div
if (childCreate){
//判定当前的下拉是不是打开状态,假如是打开的就关闭掉。是关闭的就打开。
document.getElementById("selectchild"+ obj.name).style.display="none";
childCreate=false;
}else{
document.getElementById("selectchild" + obj.name).style.display="";
childCreate=true;
}
}else{
//初始一个div放在上一个div下边,当options的替身。
var cDiv = document.createElement("div");
cDiv.id="selectchild" + obj.name;
cDiv.style.position = "absolute";
cDiv.style.width=offset.width + "px";
cDiv.style.height=obj.options.length *20 + "px";
cDiv.style.top=(offset.top+offset.height+2) + "px";
cDiv.style.left=offset.left + "px";
cDiv.style.background="#f7f7f7";
cDiv.style.border="1px solid silver";
var uUl = document.createElement("ul");
uUl.id="uUlchild" + obj.name;
uUl.style.listStyle="none";
uUl.style.margin="0";
uUl.style.padding="0";
uUl.style.fontSize="12px";
cDiv.appendChild(uUl);
document.body.appendChild(cDiv);
childCreate=true;
for (var i=0;i%26lt;obj.options.length;i++){
//将原始的select标签中的options添加到li中
var lLi=document.createElement("li");
lLi.id=obj.options[i].value;
lLi.style.textIndent="4px";
lLi.style.height="20px";
lLi.style.lineHeight="20px";
lLi.innerHTML=obj.options[i].innerHTML;
uUl.appendChild(lLi);
}
var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
for (var j=0;j%26lt;obj.options.length;j++){
//为li标签添加鼠标事件
liObj[j].onmouseover=function(){
this.style.background="gray";
this.style.color="white";
}
liObj[j].onmouseout=function(){
this.style.background="white";
this.style.color="black";
}
liObj[j].onclick=function(){
//做两件事情,一是将用户选择的保存到原始select标签中,要不做的再好看表单递交后也获取不到select的值了。
obj.options.length=0;
obj.options[0]=new Option(this.innerHTML,this.id);
//同时我们把下拉的关闭掉。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
iDiv.innerHTML=this.innerHTML;
}
}
}
}
最后这个比较复杂一点,再解释一下,我们在做这一步之前,select的样子已经出来了,下一步就是再加一个div去模拟select被点击之后出现的下拉选项了。我们可以讲select标签的options通过javascript提取出来,把它写成这样
%26lt;div%26gt;
%26lt;ul%26gt;
%26lt;li%26gt;optionName%26lt;/li%26gt;
%26lt;li%26gt;optionName%26lt;/li%26gt;
%26lt;li%26gt;optionName%26lt;/li%26gt;
%26lt;/ul%26gt;
%26lt;/div%26gt;
基本上就这样了。还有些缺陷,有时间大家可以一起补充!
