tab选择卡切换

很多页面中都会用到tab选项卡切换,下面这个巧妙的运用css设置边框,来使得激活的tab与其他未激活的边框有所不同。

就是将激活的边框的border-bottom设置为与激活tab的背景色一致,这样就看起来下面是没有边框的。

然后用jquery实现tab的切换,首先载入页面时就去掉所有的active样式,去掉所有的content内容,然后给第一个tab加上active样式,显示第一个content。然后点击对应的tab的时候,同样首先去掉所有的active样式,去掉所有的content内容,给当前点击的tab加active样式,然后通过获取当前点击tab的herf的内容来找到对应content的id并渐入显示。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>tab选项卡</title>
<script type="text/javascript" src="jquery-1.3.2.min.js" language="javascript"></script>
<style>
ul.tabs 
{
    margin
: 0;
    padding
: 0;
    float
: left;
    list-style
: none;
    height
: 32px; /*--Set height of tabs--*/
    border-bottom
: 1px solid #999;
    border-left
: 1px solid #999;
    width
: 500px;
}
ul.tabs li 
{
    float
: left;
    margin
: 0;
    padding
: 0;
    height
: 31px; /*--Subtract 1px from the height of the unordered list--*/
    line-height
: 31px; /*--Vertically aligns the text within the tab--*/
    border
: 1px solid #999;
    border-left
: none;
    margin-bottom
: -1px; /*--Pull the list item down 1px--*/
    overflow
: hidden;
    position
: relative;
    background
: #e0e0e0;
}
ul.tabs li a 
{
    text-decoration
: none;
    color
: #000;
    display
: block;
    font-size
: 1.2em;
    padding
: 0 20px;
    border
: 1px solid #fff; /*--Gives the bevel look with a 1px white border inside the list item--*/
    outline
: none;
}
ul.tabs li a:hover 
{
    background
: #ccc;
}
html ul.tabs li.active, html ul.tabs li.active a:hover  
{ /*--Makes sure that the active tab does not listen to the hover properties--*/
    background
: #fff;
    border-bottom
: 1px solid #fff; /*--Makes the active tab look like it's connected with its content--*/
}
.tab_container 
{
    border
: 1px solid #999;
    border-top
: none;
    overflow
: hidden;
    clear
: both;
    float
: left; 
    width
: 500px;
    background
: #fff;
}
.tab_content 
{
    padding
: 20px;
    font-size
: 1.2em;
}
</style>
<script>
$(
function(){
    
//进入页面时
    $(".tab_content").hide(); //候隐藏所有的content内容
    $("ul.tabs li:first").addClass("active").show(); //激活第一个tab
    $(".tab_content:first").show(); //显示第一个content内容
 
    
//点击对应的tab时
    $("ul.tabs li").click(function() {
 
        $(
"ul.tabs li").removeClass("active"); //将所有的tab去掉active样式
        $(this).addClass("active"); //点击的这个tab加active样式
        $(".tab_content").hide(); //候隐藏所有的content内容
 
        
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
 
    });       
})
</script>
</head>

<body>
<div class="container">
    
<ul class="tabs">
        
<li><href="#tab1">tab1</a></li>
        
<li><href="#tab2">tab2</a></li>
        
<li><href="#tab3">tab3</a></li>
        
<li><href="#tab4">tab4</a></li>
        
<li><href="#tab5">tab5</a></li>
    
</ul>
 
    
<div class="tab_container">
        
<div id="tab1" class="tab_content">
                Content1
        
</div>
        
<div id="tab2" class="tab_content">
                Content2
        
</div>
        
<div id="tab3" class="tab_content">
                Content3
        
</div>
        
<div id="tab4" class="tab_content">
                Content4
        
</div>
        
<div id="tab5" class="tab_content">
                Content5
        
</div>
    
</div>
</div>
</body>
</html>
原文地址:https://www.cnblogs.com/tangself/p/1675951.html