1.第一种自定义下拉菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;list-style: none;}
.selectOption{width:200px;height:30px;line-height: 30px;border:1px solid #ccc;margin:100px auto;overflow:hidden;}
.selectOption ul{}
.selectOption ul li{text-align: center;}
.selectOption ul li:hover{width:100%;background:yellow;}
.selectText{border:none;outline: none;text-align: center;width:100%;}
</style>
</head>
<body>
<div class="selectOption">
<input type="text" class="selectText" name="" value="--选项--" readonly=""></input>
<ul>
<li>下拉选项1</li>
<li>下拉选项2</li>
<li>下拉选项3</li>
<li>下拉选项4</li>
</ul>
</div>
<script type="text/javascript">
var selectOption = document.querySelector(".selectOption");//select
var selectText = document.querySelector(".selectText");//文本框
var selectOptionList = selectOption.children[1].children;
selectOption.onclick = function(e){//点击select选项
e.stopPropagation();
selectOption.style.overflow = "visible";//显示
}
document.onclick = function()
{
selectOption.style.overflow = "hidden";//隐藏
}
for(var i = 0; i < selectOptionList.length; i++)
{
selectOptionList[i].index = i;
selectOptionList[i].onclick = function(e)//select下拉选项
{
selectText.value = selectOptionList[this.index].innerHTML;//获得选中选项
e.stopPropagation();
selectOption.style.overflow = "hidden";//隐藏
}
}
</script>
</body>
</html>
2.第二种自定义下拉菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding:0;list-style: none;}
.selectBox{width:200px;height:35px;margin:150px auto;border:1px solid #ccc;}
.selectOption{position: absolute;}
.selectKuang{width:200px;height:35px;overflow:hidden;position:relative;}
.selectKuang::before{content:"";position:absolute;top:13.5px;right:12px;z-index: 10;width:0;height:0;border:8px solid transparent;border-top:8px solid #28333f;}
.selectKuang ul li {padding-left:10px;height:35px;line-height: 35px;background:orange;}
.selectKuang ul li:hover{background:red;}
.selectSpan{padding-left:10px;height:35px;line-height: 35px;background:#fff;}
</style>
</head>
<body>
<div class="selectBox">
<select name="" class="selectOption">
<option value="选项">选项</option>
<option value="选项1">选项1</option>
<option value="选项2" selected>选项2</option>
<option value="选项3">选项3</option>
</select>
<div class="selectKuang">
<div class="selectSpan">选项</div>
<ul>
<li>选项</li>
<li>选项1</li>
<li>选项2</li>
<li>选项3</li>
</ul>
</div>
</div>
<div class="selectBox">
<select name="" class="selectOption">
<option value="选项">选项</option>
<option value="选项1" selected>选项1</option>
<option value="选项2">选项2</option>
<option value="选项3">选项3</option>
</select>
<div class="selectKuang">
<div class="selectSpan">选项</div>
<ul>
<li>选项</li>
<li>选项1</li>
<li>选项2</li>
<li>选项3</li>
</ul>
</div>
</div>
<script type="text/javascript">
var selectBox = document.querySelectorAll(".selectBox");
var selectOption = document.querySelectorAll(".selectOption");
var selectOptionList = selectOption.children;
var selectKuang = document.querySelectorAll(".selectKuang");
var selectSpan = document.querySelectorAll(".selectSpan");
for(var i = 0; i < selectSpan.length; i++)
{
selectSpan[i].index = i;
selectSpan[i].onclick = function(e)
{
e.stopPropagation();
var selectKuangList = selectKuang[this.index].children[1].children;
// for(var i = 0; i < selectKuangList.length; i++)
// {
// selectKuangList[i].style.background = "";
// selectKuangList[i].style.color = "";
// }
// selectKuangList[this.index].style.background = "blue";
if(selectKuang[this.index].style.overflow == "visible")//判断是否显示
{
selectKuang[this.index].style.overflow = "hidden";//下拉菜单隐藏
}
else
{
selectKuang[this.index].style.overflow = "visible";//下拉菜单展开
}
var _this = this.index;
for(var i = 0; i < selectKuangList.length; i++)
{
selectKuangList[i].index = i;
selectKuangList[i].onclick = function()//选项子类
{
var selectValue = selectOption[_this].options[this.index].value;//下拉菜单选中哪个选项
for(var i = 0; i < selectKuangList.length; i++)
{
selectKuangList[i].style.background = "";
selectKuangList[i].style.color = "";
}
selectKuangList[this.index].style.background = "blue";
selectKuangList[this.index].style.color = "#fff";
for(var i = 0; i < selectKuangList.length; i++)
{
selectOption[_this].options[i].removeAttribute("selected");//移除未选中的选项
}
selectOption[_this].options[this.index].setAttribute("selected", true);//选中选项
selectKuang[_this].style.overflow = "hidden";//下拉隐藏
selectSpan[_this].innerHTML = selectKuangList[this.index].innerHTML;//下拉子类赋值给文本框
}
}
}
}
for(var i = 0; i < selectOption.length; i++)
{
var selectValue = selectOption[i].options[selectOption[i].selectedIndex].value;
console.log(selectOption[i].selectedInde)
selectSpan[i].innerHTML = selectValue;
// var selectOptionList = selectOption[i].children;
// for(var i = 0; i < selectOptionList.length; i++)
// {
// var bb = selectOptionList[i].getAttribute("selected")
// console.log(bb.index);
// }
// var selectKuangList = selectKuang[i].children[1].children;
// selectKuangList[i].style.background = "blue";
// selectKuangList[this.index].style.background = "blue";
// selectKuangList[this.index].style.color = "#fff";
selectOption[i].index = i;
selectOption[i].onchange = function()
{
console.log("dd:" + this.index);
var selectValue = selectOption[this.index].options[selectOption[this.index].selectedIndex].value;
selectSpan.innerHTML = selectValue;
console.log("selectValue:" + selectValue)
selectSpan[this.index].innerHTML = selectValue;
}
}
document.onclick = function()
{
for(var i = 0; i < selectKuang.length; i++){
selectKuang[i].style.overflow = "hidden";//点击任一个document隐藏
}
}
</script>
</body>
</html>
-------------------------------------------------------------------------------
第三种
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding:0;list-style: none;}
.selectBox{width:200px;height:35px;border:1px solid #ccc;margin:150px auto;}
.selectOption{position: absolute;}
.selectKuang{width:200px;height:35px;overflow:hidden;position:relative;}
.selectKuang::before{content:"";position:absolute;top:13.5px;right:10px;width:0;height:0;border:8px solid transparent;border-top:8px solid #28333f;}
.selectKuang ul{position: relative;z-index: 20;}
.selectKuang ul li {padding-left:10px;height:35px;line-height: 35px;background:orange;}
.selectKuang ul li:hover{background:red;}
.selectSpan{padding-left:10px;height:35px;line-height: 35px;background:#fff;}
.selectSpan::after{content: "";position: absolute;right: 0px;width: 36px;height: 35px;border-left: 1px solid #ccc;}
</style>
</head>
<body>
<select data-am>
<option value="选项">选项</option>
<option value="选项1">选项1</option>
<option value="选项2" selected>选项2</option>
<option value="选项3">选项3</option>
<option value="选项4">选项4</option>
</select>
<select data-am>
<option value="选项">选项</option>
<option value="选项1">选项1</option>
<option value="选项2">选项2</option>
<option value="选项3" selected>选项3</option>
<option value="选项4">选项4</option>
</select>
<select data-am>
<option value="选项">选项</option>
<option value="选项1">选项1</option>
<option value="选项2">选项2</option>
<option value="选项3">选项3</option>
<option value="选项4" selected>选项4</option>
</select>
<script type="text/javascript">
var select = document.querySelectorAll("select");
for(var i = 0; i < select.length; i++)
{
var dataAm = select[i].getAttribute("data-am");
if(dataAm == "")
{
select[i].className = "selectOption";
}
}
var selectOption = document.querySelectorAll(".selectOption");
for(var i = 0; i < selectOption.length; i++)
{
var selectBoxDiv = document.createElement("div");
selectBoxDiv.className = "selectBox";
selectOption[i].parentNode.replaceChild(selectBoxDiv, selectOption[i]);
selectBoxDiv.appendChild(selectOption[i]);
var selectKuangDiv = document.createElement("div");
var selectSpanDiv = document.createElement("div");
var selectUl = document.createElement("ul");
var selectOptionList = selectOption[i].children;
for(var j = 0; j < selectOptionList.length; j++)
{
var selectLi = document.createElement("li");
selectLi.innerHTML = selectOptionList[j].value;
selectUl.appendChild(selectLi);
}
selectKuangDiv.className = "selectKuang";
selectSpanDiv.className = "selectSpan";
// selectSpanDiv.innerHTML = "选项";
selectKuangDiv.appendChild(selectSpanDiv);
selectKuangDiv.appendChild(selectUl);
insertAfter(selectKuangDiv, selectOption[i]);
}
var selectBox = document.querySelectorAll(".selectBox");
var selectKuang = document.querySelectorAll(".selectKuang");
var selectSpan = document.querySelectorAll(".selectSpan");
function insertAfter(newElement, targetElement)
{
var parent = targetElement.parentNode;
parent.insertBefore(newElement, targetElement.nextSibling);
}
for(var i = 0; i < selectSpan.length; i++)
{
selectSpan[i].index = i;
selectSpan[i].onclick = function(e)
{
e.stopPropagation();
var selectKuangList = selectKuang[this.index].children[1].children;
if(selectKuang[this.index].style.overflow == "visible")//判断是否显示
{
selectKuang[this.index].style.overflow = "hidden";//下拉菜单隐藏
}
else
{
selectKuang[this.index].style.overflow = "visible";//下拉菜单展开
}
var _this = this.index;
for(var i = 0; i < selectKuangList.length; i++)
{
selectKuangList[i].index = i;
selectKuangList[i].onclick = function()//选项子类
{
var selectValue = selectOption[_this].options[this.index].value;//下拉菜单选中哪个选项
for(var i = 0; i < selectKuangList.length; i++)
{
selectKuangList[i].style.background = "";
selectKuangList[i].style.color = "";
}
selectKuangList[this.index].style.background = "blue";
selectKuangList[this.index].style.color = "#fff";
for(var i = 0; i < selectKuangList.length; i++)
{
selectOption[_this].options[i].removeAttribute("selected");//移除未选中的选项
}
selectOption[_this].options[this.index].setAttribute("selected", true);//选中选项
selectKuang[_this].style.overflow = "hidden";//下拉隐藏
selectSpan[_this].innerHTML = selectKuangList[this.index].innerHTML;//下拉子类赋值给文本框
}
}
}
}
for(var i = 0; i < selectOption.length; i++)
{
var selectValue = selectOption[i].options[selectOption[i].selectedIndex].value;
console.log(selectOption[i].selectedIndex)
selectSpan[i].innerHTML = selectValue;
var selectKuangList = selectKuang[i].children[1].children;
selectKuangList[selectOption[i].selectedIndex].style.background = "blue";
selectKuangList[selectOption[i].selectedIndex].style.color = "#fff";
// console.log(selectKuangList);
selectOption[i].index = i;
selectOption[i].onchange = function()
{
// console.log("dd:" + this.index);
var selectValue = selectOption[this.index].options[selectOption[this.index].selectedIndex].value;
selectSpan.innerHTML = selectValue;
// console.log("selectValue:" + selectValue)
selectSpan[this.index].innerHTML = selectValue;
}
}
document.onclick = function()
{
for(var i = 0; i < selectKuang.length; i++){
selectKuang[i].style.overflow = "hidden";//点击任一个document隐藏
}
}
</script>
</body>
</html>