首頁  >  文章  >  web前端  >  html如何實作tab頁面切換

html如何實作tab頁面切換

coldplay.xixi
coldplay.xixi原創
2021-03-03 15:56:5116462瀏覽

html實作tab頁面切換的方法:先建立一個類別名為wrap的div當作容器;然後建立四個label標籤,在每個label中建立一個span標籤;最後建立一個div作為這個導航項。

html如何實作tab頁面切換

本教學操作環境:windows7系統、html5版,DELL G3電腦。

html實作tab頁面切換的方法:

原理:透過label標籤的關聯屬性和input的單選類型實作對應div的顯示

1、建立一個類別名為wrap的div當作容器

2、建立四個label標籤,這將作為tab切換項目

3、在每個label中建立一個span標籤(導航內容),input標籤(實作選取於取消選取)type類型為radio,還要建立一個div作為這個導航項目被點中是顯示內容框,

這裡要注意的是input標籤的name必須是相同的,我這邊取名叫tab

最終HTML為下面這樣:

<div class="wrap">
    <label>
        <span>home</span>
        <input type="radio" name="tab" checked>
        <div>home-page</div>
    </label>
    <label>
        <span>list</span>
        <input type="radio" name="tab">
        <div>list-page</div>
    </label>
    <label>
        <span>news</span>
        <input type="radio" name="tab">
        <div>news-page</div>
    </label>
    <label>
        <span>mine</span>
        <input type="radio" name="tab">
        <div>mine-page</div>
    </label>
</div>

重要的css,透過將input的width設為0使得input的那個小圓點不現實,又透過label的關聯用導航項目的點擊實現input的checked,然後透過input:checked div{display:block}實現對應div的顯示

<style type="text/css">
        *{margin: 0;padding: 0;}
        .wrap{
            margin: 20px auto;
            width: 403px;
            height: 600px;
            border:1px solid brown;
            position: relative;
        }
        label{
            width: 100px;
            height: 30px;
            float: left;
            text-align: center;
            line-height:30px;
            border-right: 1px solid brown;
            border-bottom: 1px solid brown;
        }
        label:nth-of-type(4){
            border-right: none;
        }
        label span{
            cursor: pointer;
        }
        label div{
            width: 403px;
            height: 568px;
            position: absolute;
            left: 0;
            top: 31px;
            background: #eeeeee;
            display: none;
        }
        label input{
            width: 0;
        }
        input:checked+div{
            display: block;
        }
    </style>

#相關學習推薦: html影片教學

#

以上是html如何實作tab頁面切換的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn