javascript美化下拉框js

本文介绍了一种实现交互式动态下拉菜单的技术,包括隐藏真实select元素、创建虚拟div替代、显示默认选项并模拟鼠标点击操作,最终实现动态显示和隐藏下拉选项的功能。

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

Js代码 复制代码 收藏代码
  1. var childCreate = false;
  2. function Offset(e) {
  3. //取标签的绝对位置
  4. var t = e.offsetTop;
  5. var l = e.offsetLeft;
  6. var w = e.offsetWidth;
  7. var h = e.offsetHeight - 2;
  8. while ( e = e.offsetParent) {
  9. t += e.offsetTop;
  10. l += e.offsetLeft;
  11. }
  12. return {
  13. top : t,
  14. left : l,
  15. width : w,
  16. height : h
  17. }
  18. }
  19. function loadselect(obj) {
  20. //第一步:取得select所在的位置
  21. var offset = Offset(obj);
  22. //第二步:将真的select隐藏
  23. obj.style.display = "none";
  24. //第三步:虚拟一个div出来代替select
  25. var iDiv = document.createElement("div");
  26. iDiv.id = "selectof" + obj.name;
  27. iDiv.style.position = "absolute";
  28. iDiv.style.width = offset.width + "px";
  29. iDiv.style.height = offset.height + "px";
  30. iDiv.style.top = offset.top + 4 + "px";
  31. iDiv.style.left = offset.left + 4 + "px";
  32. iDiv.style.background = "url('test.png') no-repeat right -6px";
  33. iDiv.style.border = "1px solid #ccc";
  34. iDiv.style.fontSize = "12px";
  35. iDiv.style.lineHeight = offset.height + "px";
  36. iDiv.style.textIndent = "4px";
  37. document.body.appendChild(iDiv);
  38. //第四步:将select中默认的选项显示出来
  39. var tValue = obj.options[obj.selectedIndex].innerHTML;
  40. iDiv.innerHTML = tValue;
  41. //第五步:模拟鼠标点击
  42. iDiv.style.background = "url('images/select.jpg') no-repeat right -2px";
  43. iDiv.onclick = function() {//鼠标点击
  44. if (document.getElementById("selectchild" + obj.name)) {
  45. //判断是否创建过div
  46. if (childCreate) {
  47. //判断当前的下拉是不是打开状态,如果是打开的就关闭掉。是关闭的就打开。
  48. document.getElementById("selectchild" + obj.name).style.display = "none";
  49. childCreate = false;
  50. } else {
  51. document.getElementById("selectchild" + obj.name).style.display = "";
  52. childCreate = true;
  53. }
  54. } else {
  55. //初始一个div放在上一个div下边,当options的替身。
  56. var cDiv = document.createElement("div");
  57. cDiv.id = "selectchild" + obj.name;
  58. cDiv.style.position = "absolute";
  59. cDiv.style.width = offset.width + "px";
  60. cDiv.style.height = obj.options.length * 20 + "px";
  61. cDiv.style.top = (offset.top + offset.height + 2) + 4+"px";
  62. cDiv.style.left = offset.left + 4+ "px";
  63. cDiv.style.background = "#f7f7f7";
  64. cDiv.style.border = "1px solid silver";
  65. var uUl = document.createElement("ul");
  66. uUl.id = "uUlchild" + obj.name;
  67. uUl.style.listStyle = "none";
  68. uUl.style.margin = "0";
  69. uUl.style.padding = "0";
  70. uUl.style.fontSize = "12px";
  71. cDiv.appendChild(uUl);
  72. document.body.appendChild(cDiv);
  73. childCreate = true;
  74. for (var i = 0; i < obj.options.length; i++) {
  75. //将原始的select标签中的options添加到li中
  76. var lLi = document.createElement("li");
  77. lLi.id = obj.options[i].value;
  78. lLi.style.textIndent = "4px";
  79. lLi.style.height = "20px";
  80. lLi.style.lineHeight = "20px";
  81. lLi.innerHTML = obj.options[i].innerHTML;
  82. uUl.appendChild(lLi);
  83. }
  84. var liObj = document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
  85. for (var j = 0; j < obj.options.length; j++) {
  86. //为li标签添加鼠标事件
  87. liObj[j].onmouseover = function() {
  88. this.style.background = "gray";
  89. this.style.color = "white";
  90. }
  91. liObj[j].onmouseout = function() {
  92. this.style.background = "white";
  93. this.style.color = "black";
  94. }
  95. liObj[j].onclick = function() {
  96. //做两件事情,一是将用户选择的保存到原始select标签中,要不做的再好看表单递交后也获取不到select的值了。
  97. obj.options.length = 0;
  98. obj.options[0] = new Option(this.innerHTML, this.id);
  99. //同时我们把下拉的关闭掉。
  100. document.getElementById("selectchild" + obj.name).style.display = "none";
  101. childCreate = false;
  102. iDiv.innerHTML = this.innerHTML;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. document.body.onload = function (){
  109. var selects = document.getElementsByTagName("select");
  110. for(var i = 0 ; i < selects.length;i++){
  111. loadselect(selects[i]);
  112. }
  113. }
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 + 4 + "px";
	iDiv.style.left = offset.left + 4 + "px";
	iDiv.style.background = "url('test.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.style.background = "url('images/select.jpg') no-repeat right -2px";
	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) + 4+"px";
			cDiv.style.left = offset.left + 4+ "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;
				}
			}
		}
	}
}
document.body.onload = function (){
	var selects = document.getElementsByTagName("select");
	for(var i = 0 ; i < selects.length;i++){
		loadselect(selects[i]);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值