索引
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.hide{
display: none;
}
.menu{
height: 38px;
background-color: #115599;
line-height: 38px;
padding: 0 5px;
}
/*<!--active为选中时背景颜色的变色-->*/
.active{
background-color: red;
}
.menu .menu-item{
float: left;
border-right: 1px solid red;
cursor: pointer;
}
/*cursor: pointer;为鼠标在菜单时。鼠标变为其他图标*/
.content{
min-height: 50px;
border: 1px solid darkblue;
}
</style>
</head>
<body>
<div style="width: 700px;margin: 0 auto">
<div class="menu">
<div class="menu-item active" >菜单一</div>
<!--active为选中时背景颜色的变色-->
<div class="menu-item" >菜单2</div>
<div class="menu-item" >菜单3</div>
</div>
<div class="content">
<div b="1">nr1</div>
<div class="hide" >nr2</div>
<div class="hide" >nr3</div>
</div>
</div>
<script src="jquery-1.12.4.js"></script>
<script>
$('.menu-item').click(function(){
$(this).addClass('active').siblings().removeClass('active');
$('.content').children().eq($(this).index()).removeClass('hide').siblings().addClass('hide');
});
</script>
</body>
</html>
非
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .hide{ display: none; } .menu{ height: 38px; background-color: #115599; line-height: 38px; padding: 0 5px; } /*<!--active为选中时背景颜色的变色-->*/ .active{ background-color: red; } .menu .menu-item{ float: left; border-right: 1px solid red; cursor: pointer; } /*cursor: pointer;为鼠标在菜单时。鼠标变为其他图标*/ .content{ min-height: 50px; border: 1px solid darkblue; } </style> </head> <body> <div style="width: 700px;margin: 0 auto"> <div class="menu"> <div class="menu-item active" a="1">菜单一</div> <!--active为选中时背景颜色的变色--> <div class="menu-item" a="2">菜单2</div> <div class="menu-item" a="3">菜单3</div> </div> <div class="content"> <div b="1">nr1</div> <div class="hide" b="2">nr2</div> <div class="hide" b="3">nr3</div> </div> </div> <script src="jquery-1.12.4.js"></script> <script> $('.menu-item').click(function(){ $(this).addClass('active').siblings().removeClass('active'); var target = $(this).attr('a'); $('.content').children("[b='"+ target+"']").removeClass('hide').siblings().addClass('hide'); }); </script> </body> </html>