<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>.select{width: 200px;height: 40px;margin: 20px auto;}.select span{line-height: 40px;display: block;border:solid 1px slateblue;}.select ul{margin: 0;padding: 0;list-style: none;border: solid 1px slateblue;border-top: none;display: none;}.select ul li{line-height: 40px;}.select ul li.active{background: yellowgreen;color: #cccccc;}</style></head><body><select><optionvalue="北京">北京</option><optionvalue="上海">上海</option><optionvalue="广州">广州</option><optionvalue="深圳">深圳</option></select><divclass="select"><spanclass="show">北京</span><ul><liclass="active">北京</li><li>上海</li><li>广州</li><li>深圳</li></ul></div></body><script>
var ospan = document.querySelector(".select span");
var oul = document.querySelector(".select ul");
var ali = oul.children;
// 记录下拉菜单的显示或隐藏状态,0是要显示,1是要隐藏
var type = 0;
// 显示区域的点击事件
ospan.onclick = function(eve){
// 阻止冒泡
var e = eve || window.event;
// 初步判断是否要显示
if (type === 0) {
oul.style.display = "block";
// 样式的索引找内容的索引
// 根据内容设置样式索引
index = contIndex;
// 设置默认项样式
setActive();