今天给大家分享的是由图老师小编精心为您推荐的JavaScript 图标美化的下拉框,喜欢的朋友可以分享一下,也算是给小编一份支持,大家都不容易啊!
【 tulaoshi.com - Web开发 】
服务器弱智,请自行将代码中包含全角select的部分替换为半角字符。
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)程序代码
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(http://www.zcool.com.cn/pic/png5/130/pixelicious_001.png) no-repeat right -6px";
  iDiv.style.border="1px solid #ccc";
  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(http://www.zcool.com.cn/pic/png5/130/pixelicious_001.png) no-repeat right -6px";
}
iDiv.onmouseout=function(){//鼠标移走
  iDiv.style.background="url(http://www.zcool.com.cn/pic/png5/130/pixelicious_001.png) no-repeat right -6px";
}
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;iobj.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;
来源:http://www.tulaoshi.com/n/20160219/1610533.html
看过《JavaScript 图标美化的下拉框》的人还看了以下文章 更多>>