方法一:运用a链接锚点实现 + jquery实现按钮按下变色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript"src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script><!--导入jquery库-->
<title></title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
body{
padding: 50px;
}
a{
list-style: none;
text-decoration: none;
color: #000000;
display: inline-block;
width: 60px;
height: 20px;
font-size: 14px;
text-align: center;
}
.content{
width: 200px;
height: 200px;
overflow: hidden;
}
#a1{
width: 200px;
height: 200px;
background: greenyellow;
}
#a2{
width: 200px;
height: 200px;
background: orange;
}
.end{
background: blue ;
color: #fff;
}
</style>
<script type="text/javascript">
$(function(){
$("a").click(function(){
$("a").eq($(this).index()).addClass("end").siblings().removeClass("end");
})
})
</script>
</head>
<body>
<div>
<a href="#a1" class="end">第一页</a>
<a href="#a2">第二页</a>
<div class="content">
<div id="a1">
</div>
<div id="a2">
</div>
</div>
</body>
</html>
方法二:用input按钮和:checked选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.tab{
width: 300px;
height: 150px;
margin-left: 30px;
margin-top: 30px;
border: 1px solid #eee;
position: relative;
overflow: hidden;
}
input[type='radio']{
display: none;
}
.tab label{
display: block;
cursor: pointer;
position: absolute;
width: 60px;
height: 30px;
line-height: 30px;
text-align: center;
border: 1px solid #eee;
}
.one{
left: 0;
top: 0;
}
.two{
left: 60px;
top: 0;
}
input[type='radio']:checked~div[class^='mod']{
display: block;
}
input[type='radio']:checked~label{
background: green;
color: #fff;
}
[class^='mod']{/*[class^='']匹配属性值以指定值开头的每个元素*/
position: absolute;
top: 40px;
left: 20px;
display: none;
}
.mod-1{
width: 200px;
height: 80px;
background: blueviolet;
}
.mod-2{
width: 200px;
height: 80px;
background: yellow;
}
</style>
</head>
<body>
<div class="tab">
<div>
<input type="radio" id="rd" name="tab" checked>
<label for="rd" class="one">第一张</label>
<div class="mod-1">
</div>
</div>
<div>
<input type="radio" id="rds" name="tab">
<label for="rds" class="two">第二张</label>
<div class="mod-2">
</div>
</div>
</div>
</body>
</html>
补充:还有一些常用的css属性选择器如下:
-
[attribute~=value] 选取属性值中包含指定词汇的元素
-
[attribute|=value] 选取带有以指定值开头的属性值的元素,该值必须是整个单词。
-
[attribute$=value] 匹配属性值以指定值结尾的每个元素
-
[attribute*=value] 匹配属性值中包含指定值的每个元素