首页 > 代码库 > 原生js选项卡(代码临时存储)

原生js选项卡(代码临时存储)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>
<title>js函数</title>
</head>
<body>
<style>
#div0 div{
width: 100px;
background: red;
height: 100px;
border: 1px solid #b2b2b2;
margin-bottom: 10px;
display: none;
}
.active{
background:yellow;
}
</style>
<div id="div0">
<input type="button" value="http://www.mamicode.com/新闻" class="active">
<input type="button" value="http://www.mamicode.com/音乐">
<input type="button" value="http://www.mamicode.com/游戏">
<input type="button" value="http://www.mamicode.com/广告">
<div style="display: block;margin-top: 10px;">新闻</div>
<div>音乐</div>
<div>游戏</div>
<div>广告</div>
</div>

<script>
var div0=document.getElementById(‘div0‘)
var ainput=div0.getElementsByTagName(‘input‘);
var adiv1=div0.getElementsByTagName(‘div‘);
for(var i=0;i<ainput.length;i++){
ainput[i].index=i
ainput[i].onclick=function () {
for(var j=0;j<ainput.length;j++){
ainput[j].className=‘‘;
adiv1[j].style.display=‘none‘;
}
this.className=‘active‘
adiv1[this.index].style.display=‘block‘
}
}
</script>
</body>
</html>

技术分享

原生js选项卡(代码临时存储)