梦幻屋

软件搜索:

用javascript实现select的美化

更新时间: 2007-6-6 10:26:00 来源: 蓝色理想  作者: 佚名 Add to ToLib.com...
  •   论坛经常有人会问到用CSS如何美化Select标签,其实但凡你看到很酷的都是用javascript来......

第一步:把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<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<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提取出来,把它写成这样:

<div>
    <ul>
        <li>optionName</li>
        <li>optionName</li>
        <li>optionName</li>
    </ul>
</div>

基本上就这样了。还有些缺陷,有时间大家可以一起补充!

上一页  [1] [2] 

梦幻屋
  • 上一篇文章:

  • 此栏目下没有文章
  • 热点推荐

    女性推荐
    排行榜

    梦幻屋学院
    设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 |  
    Copyright© 2005-2006 梦幻工作室版权所有. All rights reserved.
    经营许可证编号:蜀ICP备07002423