隨筆-51  評論-14  文章-0  trackbacks-0
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
           
          <head>
           
          <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
           
          <meta http-equiv="Content-Language" content="zh-CN" />
           
          <title>用javascript美化Select</title>
           
          <script type="text/javascript">
          var childCreate
          =false;
          function Offset(e)
          //取標簽的絕對位置
          {
           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
           }
          }
          function loadSelect(obj){
           
          //第一步:取得Select所在的位置
           var offset=Offset(obj);
           
          //第二步:將真的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(/articleimg/2007/04/4687/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中默認的選項顯示出來
           var tValue=obj.options[obj.selectedIndex].innerHTML;
           iDiv.innerHTML
          =tValue;
           
          //第五步:模擬鼠標點擊
           iDiv.onmouseover=function(){//鼠標移到
            iDiv.style.background="url(/articleimg/2007/04/4687/icon_select_focus.gif) no-repeat right 4px";
           }
           iDiv.onmouseout
          =function(){//鼠標移走
            iDiv.style.background="url(/articleimg/2007/04/4687/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;
              }
             }
            }
           }
          }
                  
          </script>
           
          <style type="text/css">
           select{width:200px;height:20px;}
          </style>
           
          </head>
           
          <body>
            
          <h1>用javascript模擬select達到美化效果</h1>
          <form name="f">
           
          <fieldset>
            
          <legend>用戶注冊</legend>
            
          <div>
             
          <label for="username">帳號</label>
             
          <input type="text" id="username" name="username" />
            
          </div>
            
          <div>
             
          <label for="pwd">密碼</label>
             
          <input type="password" name="pwd" id="pwd" />
            
          </div>
            
          <div>
             
          <label for="province">省份</label>
             
          <select id="province" name="province">
              
          <option value="10">江西</option>
              
          <option value="11">福建</option>
              
          <option value="12">廣東</option>
              
          <option value="13">浙江</option>
             
          </select>
            
          </div>
           
          </fieldset>
           
          <input type="submit" value="提交" name="btnSub" />
          </form>
            
          <script type="text/javascript">
             loadSelect(document.f.province);
            
          </script>
           
          <p>
            
          <a href="http://www.iwcn.net">作者博客</a>
           
          </p>
           
          </body>
          </html>
          <a href="http://js.alixixi.com/">歡迎訪問阿里西西網頁特效代碼站,js.alixixi.com</a>
          posted on 2008-06-18 15:21 Hank1026 閱讀(3428) 評論(1)  編輯  收藏 所屬分類: 每日積累

          評論:
          # re: 使用js美化下拉列表樣式 2011-04-02 18:52 | 云清
          圖片在什么地方呀  回復  更多評論
            
          主站蜘蛛池模板: 紫金县| 萍乡市| 葫芦岛市| 墨玉县| 铜川市| 山西省| 鹤山市| 田东县| 霍邱县| 浦城县| 五莲县| 右玉县| 柏乡县| 张北县| 安泽县| 桦甸市| 嘉禾县| 丰城市| 阳高县| 蒙自县| 安庆市| 山东| 吴堡县| 东安县| 响水县| 宁蒗| 鄢陵县| 香河县| 治县。| 屯门区| 广丰县| 合作市| 德清县| 瑞丽市| 东丽区| 马龙县| 麻栗坡县| 前郭尔| 余姚市| 齐河县| 萨嘎县|