码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript 简单切换效果

时间:2016-05-10 02:03:02      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tab</title>
<style type="text/css">
* {padding:0; margin:0;}
button {
background-color: #ccc;
width:80px;
height: 30px;
}
.btn-active {
background-color: yellow;
font-weight:bold;
font-size: 14px;
}
#dd div{
width:200px;
height: 200px;
font-size: 64px;
background-color: #0c0;
display: none;
color:#fff;
}
#dd .div-active {
display: block;
}

</style>
</head>
<body>
<button class="btn-active">按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
<button>按钮4</button>
<div id="dd">
<div class="div-active">1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>

<script type="text/javascript">
//1.先获取元素
var buttonList = document.getElementsByTagName("button");
var divList = document.getElementById("dd").getElementsByTagName("div");
//2.添加事件
for(var i = 0; i < buttonList.length; i++) {
buttonList[i].index = i;
buttonList[i].onclick = function() {
for(var j = 0; j < buttonList.length;j++) {
buttonList[j].className = "";
// divList[j].className = "";
//或下面写法
divList[j].style.display = "none";
}
buttonList[this.index].className = "btn-active";
// divList[this.index].className = "div-active";
//或下面写法
divList[this.index].style.display = "block";
}
}

</script>
</body>
</html>

Javascript 简单切换效果

标签:

原文地址:http://www.cnblogs.com/fengzj/p/5476160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!