js模拟下拉菜单

JavaScript模拟下拉菜单

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			
			li {
				list-style: none;
			}
			
			#box {
				width: 200px;
				height: 25px;
				background: #f00;
				color: #fff;
				margin: 20px auto;
				cursor: pointer;
			}
			
			#box01 {
				width: 200px;
				height: 135px;
				background: #f1f1f1;
				margin: 0 auto;
				display: none;
			}
			
			#box01 li {
				width: 200px;
				height: 25px;
				background: #f00;
				color: #fff;
				border-bottom: 1px dashed #cecece;
				cursor: pointer;
			}
		</style>
	</head>

	<body>
		<div id="box">
			<span>请选择游戏名称</span>
		</div>
		<div id="box01">
			<ul>
				<li>地下城与勇士</li>
				<li>魔兽世界(国服)</li>
				<li>魔兽世界(台服)</li>
				<li>热血江湖</li>
				<li>大话西游2</li>
				<li>QQ幻想世界</li>
			</ul>
		</div>

		<script type="text/javascript">
			window.onload = function() {
				var oBox = document.getElementById("box");
				var aBox = document.getElementById("box01");
				var oLi = document.getElementsByTagName("li");

				oBox.onclick = function(e) {
					aBox.style.display = "block";
					var evt = e || event;
					if(evt.stopPropagation) {
						evt.stopPropagation();
					} else {
						evt.cancelable = true;
					}
					for(var i = 0; i < oLi.length; i++) {
						oLi[i].onclick = function() {
							oBox.innerHTML = this.innerHTML;
							aBox.style.display = "none"
							oBox.style.background = "#0f0";

						}
						oLi[i].onmouseover = function() {
							this.style.background = "#8b8b8b";
						}
						oLi[i].onmouseout = function() {
							this.style.background = "#f00";
						}
					}
					document.onclick=function(){
						aBox.style.display="none";
					}
				}
			}
		</script>
	</body>

</html>

有任何错误,敬请指教!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值