css版本1⃣️:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>按照顺序传递参数</title>
<style>
h2 {
border-top: solid cornflowerblue 1px;
border-left: solid cornflowerblue 1px;
width: 50px;
height: 25px;
margin: 0;
float: left;
text-align: center;
}
.tab-content {
border: solid cornflowerblue 1px;
width: 152px;
height: 100px;
}
.tab-content div{
display: none;
}
.selected {
background-color: cornflowerblue;
}
.tab-content .show{
display: block;
}
</style>
</head>
<body>
<div class="tab-head">
<h2 class="selected c1 c2 c3">1</h2>
<h2 >2</h2>
<h2 >3</h2>
</div>
<div class="tab-content">
<div class="show">content1</div>
<div>content2</div>
<div>content3</div>
</div>
</body>
<script>
var tabs=document.getElementsByClassName("tab-head")[0].getElementsByTagName("h2"),
contents=document.getElementsByClassName("tab-content")[0].getElementsByTagName("div");
for(var i=0;i<tabs.length;i++){
tabs[i].onmouseover=function(){
for(var i=0;i<contents.length;i++){
if (tabs[i] == this) {
tabs[i].classList.add('selected');
contents[i].classList.add('show');
} else {
tabs[i].classList.remove('selected');
contents[i].classList.remove('show');
}
}
}
}
</script>
</html>
css版本2⃣️:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>标签栏切换效果</title>
<style>
body,
ul {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.tab-box {
width: 383px;
margin: 10px;
border: 1px solid #ccc;
border-top: 2px solid #222222;
}
.tab-head {
height: 31px;
}
.tab-head-div {
width: 95px;
height: 30px;
float: left;
border-bottom: 1px solid #ccc;
border-right: 1px solid #ccc;
background: #0cc6f5;
line-height: 30px;
text-align: center;
cursor: pointer;
color: #fff;
}
.tab-head .current {
background: #fff;
border-bottom: 1px solid #fff;
color: #000;
}
.tab-head-r {
border-right: 0;
}
.tab-body-div {
display: none;
margin: 20px 10px;
}
.tab-body .current {
display: block;
}
</style>
</head>
<body>
<div class="tab-box">
<div class="tab-head">
<div class="tab-head-div current">标签一</div>
<div class="tab-head-div">标签二</div>
<div class="tab-head-div">标签三</div>
<div class="tab-head-div tab-head-r">标签四</div>
</div>
<div class="tab-body">
<div class="tab-body-div current"> a </div>
<div class="tab-body-div"> b </div>
<div class="tab-body-div"> c </div>
<div class="tab-body-div"> d </div>
</div>
</div>
<script>
var tabs = document.getElementsByClassName('tab-head-div');
var divs = document.getElementsByClassName('tab-body-div');
for (var i = 0; i < tabs.length; ++i) {
tabs[i].onmouseover = function () {
for (var i = 0; i < divs.length; ++i) {
if (tabs[i] == this) {
divs[i].classList.add('current');
tabs[i].classList.add('current');
} else {
divs[i].classList.remove('current');
tabs[i].classList.remove('current');
}
}
};
}
</script>
</body>
<html>
css版本3⃣️:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.tap_head{
width: 390px;
height: 80px;
border: 1px solid black;
background-color: blanchedalmond;
margin: 10px;
}
.tap_head .current{
color: #000;
}
.tap_head_div{
width: 95px;
height: 30px;
float: left;
border-bottom: 1px solid #ccc;
border-right: 1px solid #ccc;
background: #0cc6f5;
line-height: 30px;
text-align: center;
color: #fff;
}
.tap_body_div{
display: none;
}
.tap-body .current{
display: block;
}
</style>
</head>
<body>
<div class="tap_head">
<div class="tap">
<div class="tap_head_div current">标签一</div>
<div class="tap_head_div">标签二</div>
<div class="tap_head_div">标签三</div>
<div class="tap_head_div">标签四</div>
</div>
<div class="tap-body">
<div class="tap_body_div current">one</div>
<div class="tap_body_div">two</div>
<div class="tap_body_div">three</div>
<div class="tap_body_div">four</div>
</div>
</div>
</body>
<script>
var tap_div=document.getElementsByClassName("tap_head_div");
var tap_body=document.getElementsByClassName("tap_body_div");
for(var i=0;i<tap_div.length;i++){
tap_div[i].onmouseover=function(){
for(var i=0;i<tap_body.length;i++){
if(tap_div[i]==this){
tap_div[i].classList.add("current");
tap_body[i].classList.add("current");
}else{
tap_div[i].classList.remove("current");
tap_body[i].classList.remove("current");
}
}
}
}
</script>
<html>
css版本4⃣️——升级版:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
.tab {
width: 978px;
margin: 100px auto;
}
.tab_list {
height: 39px;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
.tab_list li {
float: left;
height: 39px;
line-height: 39px;
padding: 0 20px;
text-align: center;
cursor: pointer;
}
.tab_list .current {
background-color: #c81623;
color: #fff;
}
.item_info {
padding: 20px 0 0 20px;
}
.item {
display: none;
}
</style>
</head>
<body>
<div class="tab">
<div class="tab_list">
<ul>
<li class="current">商品介绍</li>
<li>规格与包装</li>
<li>售后保障</li>
<li>商品评价(50000)</li>
<li>手机社区</li>
</ul>
</div>
<div class="tab_con">
<div class="item" style="display: block;">商品介绍模块内容</div>
<div class="item">规格与包装模块内容</div>
<div class="item">售后保障模块内容</div>
<div class="item">商品评价(50000)模块内容</div>
<div class="item">手机社区模块内容</div>
</div>
</div>
<script>
var tab_list = document.querySelector('.tab_list');
var lis = tab_list.querySelectorAll('li');
var items = document.querySelectorAll('.item');
for (var i = 0; i < lis.length; i++) {
lis[i].setAttribute('index', i);
lis[i].onclick = function () {
for (var i = 0; i < lis.length; i++) {
lis[i].className = '';
}
this.className = 'current';
var index = this.getAttribute('index');
for (var i = 0; i < items.length; i++) {
items[i].style.display = 'none';
}
items[index].style.display = 'block';
};
}
</script>
</body>
</html>
js实现版本——极为简单实用:
<!DOCTYPE html>
<html lang="en">
<script src="../js/jquery-1.12.4.js"></script>
<head>
<meta charset="UTF-8">
<title>按照顺序传递参数</title>
<style>
h2 {
border-top: solid cornflowerblue 1px;
border-left: solid cornflowerblue 1px;
width: 50px;
height: 25px;
margin: 0;
float: left;
text-align: center;
}
.tab-content {
border: solid cornflowerblue 1px;
width: 152px;
height: 100px;
}
.tab-content div{
display: none;
}
.selected {
background-color: cornflowerblue;
}
</style>
</head>
<body>
<div class="tab-head">
<h2 class="selected">1</h2>
<h2 >2</h2>
<h2 >3</h2>
</div>
<div class="tab-content">
<div style="display: block;">content1</div>
<div>content2</div>
<div>content3</div>
</div>
</body>
<script>
$(".tab-head h2").mouseover(function(){
$(this).addClass("selected").siblings().removeClass("selected");
var index = $(this).index();
$(".tab-content div").eq(index).show().siblings().hide();
});
</script>
</html>