用javascript模拟的select选择框

本文介绍了一种使用JavaScript来美化HTML中Select元素的方法。通过隐藏原始的Select框并用自定义的样式替代,可以实现更加美观且交互丰富的下拉选择菜单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <meta http-equiv="Content-Language" content="zh-CN" />
  6. <title>用javascript美化Select</title>
  7. <script type="text/javascript">
  8. var childCreate=false;
  9. function Offset(e)
  10. //取标签的绝对位置
  11. {
  12. var t = e.offsetTop;
  13. var l = e.offsetLeft;
  14. var w = e.offsetWidth;
  15. var h = e.offsetHeight-2;
  16. while(e=e.offsetParent)
  17. {
  18. t+=e.offsetTop;
  19. l+=e.offsetLeft;
  20. }
  21. return {
  22. top : t,
  23. left : l,
  24. width : w,
  25. height : h
  26. }
  27. }
  28. function loadSelect(obj){
  29. //第一步:取得Select所在的位置
  30. var offset=Offset(obj);
  31. //第二步:将真的select隐藏
  32. obj.style.display="none";
  33. //第三步:虚拟一个div出来代替select
  34. var iDiv = document.createElement("div");
  35. iDiv.id="selectof" + obj.name;
  36. iDiv.style.position = "absolute";
  37. iDiv.style.width=offset.width + "px";
  38. iDiv.style.height=offset.height + "px";
  39. iDiv.style.top=offset.top + "px";
  40. iDiv.style.left=offset.left + "px";
  41. iDiv.style.background="url(/articleimg/2007/04/4687/icon_select.gif) no-repeat right 4px";
  42. iDiv.style.border="1px solid #3366ff";
  43. iDiv.style.fontSize="12px";
  44. iDiv.style.lineHeight=offset.height + "px";
  45. iDiv.style.textIndent="4px";
  46. document.body.appendChild(iDiv);
  47. //第四步:将select中默认的选项显示出来
  48. var tValue=obj.options[obj.selectedIndex].innerHTML;
  49. iDiv.innerHTML=tValue;
  50. //第五步:模拟鼠标点击
  51. iDiv.onmouseover=function(){//鼠标移到
  52. iDiv.style.background="url(/articleimg/2007/04/4687/icon_select_focus.gif) no-repeat right 4px";
  53. }
  54. iDiv.onmouseout=function(){//鼠标移走
  55. iDiv.style.background="url(/articleimg/2007/04/4687/icon_select.gif) no-repeat right 4px";
  56. }
  57. iDiv.onclick=function(){//鼠标点击
  58. if (document.getElementById("selectchild" + obj.name)){
  59. //判断是否创建过div
  60. if (childCreate){
  61. //判断当前的下拉是不是打开状态,如果是打开的就关闭掉。是关闭的就打开。
  62. document.getElementById("selectchild" + obj.name).style.display="none";
  63. childCreate=false;
  64. }else{
  65. document.getElementById("selectchild" + obj.name).style.display="";
  66. childCreate=true;
  67. }
  68. }else{
  69. //初始一个div放在上一个div下边,当options的替身。
  70. var cDiv = document.createElement("div");
  71. cDiv.id="selectchild" + obj.name;
  72. cDiv.style.position = "absolute";
  73. cDiv.style.width=offset.width + "px";
  74. cDiv.style.height=obj.options.length *20 + "px";
  75. cDiv.style.top=(offset.top+offset.height+2) + "px";
  76. cDiv.style.left=offset.left + "px";
  77. cDiv.style.background="#f7f7f7";
  78. cDiv.style.border="1px solid silver";
  79. var uUl = document.createElement("ul");
  80. uUl.id="uUlchild" + obj.name;
  81. uUl.style.listStyle="none";
  82. uUl.style.margin="0";
  83. uUl.style.padding="0";
  84. uUl.style.fontSize="12px";
  85. cDiv.appendChild(uUl);
  86. document.body.appendChild(cDiv);
  87. childCreate=true;
  88. for (var i=0;i<obj.options.length;i++){
  89. //将原始的select标签中的options添加到li中
  90. var lLi=document.createElement("li");
  91. lLi.id=obj.options[i].value;
  92. lLi.style.textIndent="4px";
  93. lLi.style.height="20px";
  94. lLi.style.lineHeight="20px";
  95. lLi.innerHTML=obj.options[i].innerHTML;
  96. uUl.appendChild(lLi);
  97. }
  98. var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
  99. for (var j=0;j<obj.options.length;j++){
  100. //为li标签添加鼠标事件
  101. liObj[j].onmouseover=function(){
  102. this.style.background="gray";
  103. this.style.color="white";
  104. }
  105. liObj[j].onmouseout=function(){
  106. this.style.background="white";
  107. this.style.color="black";
  108. }
  109. liObj[j].onclick=function(){
  110. //做两件事情,一是将用户选择的保存到原始select标签中,要不做的再好看表单递交后也获取不到select的值了。
  111. obj.options.length=0;
  112. obj.options[0]=new Option(this.innerHTML,this.id);
  113. //同时我们把下拉的关闭掉。
  114. document.getElementById("selectchild" + obj.name).style.display="none";
  115. childCreate=false;
  116. iDiv.innerHTML=this.innerHTML;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. </script>
  123. <style type="text/css">
  124. select{width:200px;height:20px;}
  125. </style>
  126. </head>
  127. <body>
  128. <h1>用javascript模拟select达到美化效果</h1>
  129. <form name="f">
  130. <fieldset>
  131. <legend>用户注册</legend>
  132. <div>
  133. <label for="username">帐号</label>
  134. <input type="text" id="username" name="username" />
  135. </div>
  136. <div>
  137. <label for="pwd">密码</label>
  138. <input type="password" name="pwd" id="pwd" />
  139. </div>
  140. <div>
  141. <label for="province">省份</label>
  142. <select id="province" name="province">
  143. <option value="10">江西</option>
  144. <option value="11">福建</option>
  145. <option value="12">广东</option>
  146. <option value="13">浙江</option>
  147. </select>
  148. </div>
  149. </fieldset>
  150. <input type="submit" value="提交" name="btnSub" />
  151. </form>
  152. <script type="text/javascript">
  153. loadSelect(document.f.province);
  154. </script>
  155. <p>
  156. <a href="http://www.iwcn.net">作者博客</a>
  157. </p>
  158. </body>
  159. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值