div做下拉列表

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
*{ margin:0px auto; padding:0px;}
#sel{ width:280px; height:40px; border:1px solid #C0F;
margin-top:100px; line-height:40px; vertical-align:middle; padding-left:20px;}
#list{ width:300px; height:200px; border-left:1px solid#c0f; border-right:1px solid#c0f;
}
.item{ width:280px; height:39px; border-bottom:1px solid#c0f; line-height:40px; vertical-align:middle; padding-left:20px;}
</style>
</head>

<body>

<div id="sel"></div>
<div id="list" style="display:none">
<div class="item" onmouseover="xz(this)" onclick="xuanzhong(this)">北京</div>
<div class="item" onmouseover="xz(this)" onclick="xuanzhong(this)">上海</div>
<div class="item" onmouseover="xz(this)" onclick="xuanzhong(this)">南京</div>
<div class="item" onmouseover="xz(this)" onclick="xuanzhong(this)">重庆</div>
<div class="item" onmouseover="xz(this)" onclick="xuanzhong(this)">深圳</div>
</div>

<!--<select>
<option>11</option>
<option>22</option>
<option>33</option>
</select>-->

</body>
<script type="text/javascript">

function xz(a){
//所有的还原
var sy =document.getElementsByClassName("item");
for(var i=0;i<sy.length;i++){
sy[i].style.backgroundColor="white";
sy[i].style.color = "black";
}
//当前的选中
a.style.backgroundColor="blue";
a.style.color = "white";
}

var sel = document.getElementById("sel");
sel.onclick = function(){

var c = document.getElementById("list");
if(c.style.display=="none"){
c.style.display="block";
}else{
c.style.display="none";
}
}

function xuanzhong(a){
//隐藏
document.getElementById("list").style.display="none";
//取值
document.getElementById("sel").innerText = a.innerText;
}

</script>
</html>

posted @ 2017-06-19 23:07  折剑公子  阅读(114)  评论(0编辑  收藏  举报