这篇文章主要介绍了javascript实现tab切换的四种方法,并且对每个方法进行了评价,感兴趣的小伙伴们可以参考一下
tab切换在网页中很常见,故最近总结了4种实现方法。
首先,写出tab的框架,加上最简单的样式,代码如下:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style>
*{
padding: 0;
margin: 0;
}
li{
list-style: none;
float:left;
}
#tabcon{
clear: both;
}
</style>
</head>
<body>
<p id="tancontainer">
<p id="tab">
<ul>
<li>标题一</li>
<li>标题二</li>
<li>标题三</li>
<li>标题四</li>
</ul>
</p>
<p id="tabcon">
<p>内容一</p>
<p>内容二</p>
<p>内容三</p>
<p>内容四</p>
</p>
</p>
</body>
</html>
现在的显示效果如下图:
四个tab标题和四个内容区都显示在了页面中,现在要实现tab切换效果,即点击标题一,内容一显示出来,其他内容不显示;点击标题二,内容二显示出来,其他内容不显示……
那么,整体思路很简单,给四个标题绑定事件,触发的时候对应的内容显示,其他的内容隐藏。
方法一:点击标题对应的内容显示,非点击标题对应的内容隐藏。代码如下:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style>
*{
padding: 0;
margin: 0;
}
li{
list-style: none;
}
</style>
<script>
function tab(pid){
var tabs=["tab1","tab2","tab3","tab4"];
for(var i=0;i<4;i++){
if(tabs[i]==pid){
document.getelementbyid(tabs[i]).style.display="block";
}else{
document.getelementbyid(tabs[i]).style.display="none";
}
}
}
</script>
</head>
<body>
<p id="tancontainer">
<p id="tabnav">
<ul>
<li onclick="tab('tab1')">标题一</li>
<li onclick="tab('tab2')">标题二</li>
<li onclick="tab('tab3')">标题三</li>
<li onclick="tab('tab4')">标题四</li>
</ul>
</p>
<p id="tab">
<p id="tab1">内容一</p>
<p id="tab2">内容二</p>
<p id="tab3">内容三</p>
<p id="tab4">内容四</p>
</p>
</p>
</body>
</html>
方法二:先设置所有内容隐藏,然后点击标题对用的内容显示。代码如下:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style>
*{
padding: 0;
margin: 0;
}
li{
list-style: none;
float:left;
}
#tabcon{
clear: both;
}
#tabcon_1{
display: none;
}
#tabcon_2{
display: none;
}
#tabcon_3{
display: none;
}
</style>
<script>
function changetab(tabcon_num){
for(i=0;i<=3;i++) {
document.getelementbyid("tabcon_"+i).style.display="none"; //将所有的层都隐藏
}
document.getelementbyid("tabcon_"+tabcon_num).style.display="block";//显示当前层
}
</script>
</head>
<body>
<p id="tancontainer">
<p id="tab">
<ul>
<li onclick="changetab('0')">标题一</li>
<li onclick="changetab('1')">标题二</li>
<li onclick="changetab('2')">标题三</li>
<li onclick="changetab('3')">标题四</li>
</ul>
</p>
<p id="tabcon">
<p id="tabcon_0">内容一</p>
<p id="tabcon_1">内容二</p>
<p id="tabcon_2">内容三</p>
<p id="tabcon_3">内容四</p>
</p>
</p>
</body>
</html>
方法三:显示和隐藏通过是有拥有class控制,先把所有的内容隐藏dispaly设为none,而该class的display设置为block,遍历所有标题节点和内容节点,点击标题后,该标题节点和对用的内容节点拥有class,其他的没有。代码如下:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style>
*{
padding: 0;
margin: 0;
}
li{
list-style: none;
float:left;
}
#tabcon {
clear: both;
}
#tabcon p {
display:none;
}
#tabcon p.fp {
display:block;
}
</style>
</head>
<body>
<p id="tancontainer">
<p id="tab">
<ul>
<li class="fli">标题一</li>
<li>标题二</li>
<li>标题三</li>
<li>标题四</li>
</ul>
</p>
<p id="tabcon">
<p class="fp">内容一</p>
<p>内容二</p>
<p>内容三</p>
<p>内容四</p>
</p>
</p>
</body>
<script>
var tabs=document.getelementbyid("tab").getelementsbytagname("li");
var ps=document.getelementbyid("tabcon").getelementsbytagname("p");
for(var i=0;i<tabs.length;i++){
tabs[i].onclick=function(){change(this);}
}
function change(obj){
for(var i=0;i<tabs.length;i++){
if(tabs[i]==obj){
tabs[i].classname="fli";
ps[i].classname="fp";
}else{
tabs[i].classname="";
ps[i].classname="";
}
}
}
</script>
</html>
该方法的缺点是,内容块的p下面不能再有p标签了。
方法四:不用js,用“input:checked”来做tab切换,先把所有的内容隐藏,被选中的内容显示。代码如下:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>input:checked实现tab切换</title>
<style>
input{
opacity: 0;/*隐藏input的选择框*/
}
label{
cursor: pointer;/*鼠标移上去变成手状*/
float: left;
}
label:hover{
background: #eee;
}
input:checked+label{
color: red;
}
/*选择前面有.tabs input:nth-of-type(x):checked的.panels .panel:nth-child(x)*/
.tabs input:nth-of-type(1):checked~.panels .panel:nth-child(1),
.tabs input:nth-of-type(2):checked~.panels .panel:nth-child(2){
opacity: 1;
}
.panel{
opacity: 0;
position: absolute;/*使内容区域位置一样*/
}
</style>
</head>
<body>
<p class="tabs">
<input checked id="one" name="tabs" type="radio">
<label for="one">标题一</label>
<input id="two" name="tabs" type="radio">
<label for="two">标题二</label>
<p class="panels">
<p class="panel">
<p>内容一</p>
</p>
<p class="panel">
<p>内容二</p>
</p>
</p>
</p>
</body>
</html>
该方法的缺点是,不同区域切换只能通过点击。
以上就是为大家总结的tab切换实现方法,希望对大家的学习有所帮助,顺着这个思路动手制作自己tab切换特效。
更多javascript实现tab切换的四种方法。