HTML / CSS选项卡显示唯一内容

时间:2014-03-21 06:14:34

标签: html css

对于用户点击的每个标签,我需要显示不同的内容。我必须坚持布局保持不变。我的CSS如下:

#header ul {
    list-style: none;
    padding:0;
    margin:0;
}


#header li {
    float: left;
    border: 1px solid;
    border-bottom-width: 0;
    margin: 0 0.5em 0 0;
}

#header li a {
     padding: 0 1em;
}

#content {
    border: 1px solid;
    clear: both;
}

h1 {
    margin: 0;
    padding: 0 0 1em 0;
} 



#header #selected {
    position: relative;
    top: 1px;
    background: white;
}

我的HTML文件如下:

<head>
<link rel="stylesheet" href="style/style-style.css">
</head>


<div id="header"> 

<h1><center>Tabs</center></h1>
<ul>
    <li><a href="#">This</a></li>
    <li id="selected"><a href="#">That</a></li>
    <li><a href="#">The Other</a></li>
    <li><a href="#">Banana</a></li>
</ul>

</div>

<div id="content">
    <p>Ispum schmipsum.</p>
</div>

同样,我的问题是如何根据用户可能点击的标签(This,That,The Other,Banana)显示唯一内容。

3 个答案:

答案 0 :(得分:1)

做这样的事

您的HTML代码

<ul class="tabs">
        <li>
          <input type="radio" checked name="tabs" id="tab1">
          <label for="tab1">tab 1</label>
          <div id="tab-content1" class="tab-content animated fadeIn">
    ...
          </div>
        </li>
        <li>
          <input type="radio" name="tabs" id="tab2">
          <label for="tab2">tab 2</label>
          <div id="tab-content2" class="tab-content animated fadeIn">
            ...
          </div>
        </li>
        <li>
          <input type="radio" name="tabs" id="tab3">
          <label for="tab3">tab 3</label>
          <div id="tab-content3" class="tab-content animated fadeIn">
            ...
          </div>
        </li>
</ul>

你的css

body, html {
          height: 100%;
          margin: 0;
          -webkit-font-smoothing: antialiased;
          font-weight: 100;
          background: #aadfeb;
          text-align: center;
          font-family: helvetica;
      }

      .tabs input[type=radio] {
          position: absolute;
          top: -9999px;
          left: -9999px;
      }
      .tabs {
        width: 650px;
        float: none;
        list-style: none;
        position: relative;
        padding: 0;
        margin: 75px auto;
      }
      .tabs li{
        float: left;
      }
      .tabs label {
          display: block;
          padding: 10px 20px;
          border-radius: 2px 2px 0 0;
          color: #08C;
          font-size: 24px;
          font-weight: normal;
          font-family: 'Lily Script One', helveti;
          background: rgba(255,255,255,0.2);
          cursor: pointer;
          position: relative;
          top: 3px;
          -webkit-transition: all 0.2s ease-in-out;
          -moz-transition: all 0.2s ease-in-out;
          -o-transition: all 0.2s ease-in-out;
          transition: all 0.2s ease-in-out;
      }
      .tabs label:hover {
        background: rgba(255,255,255,0.5);
        top: 0;
      }

      [id^=tab]:checked + label {
        background: #08C;
        color: white;
        top: 0;
      }

      [id^=tab]:checked ~ [id^=tab-content] {
          display: block;
      }
      .tab-content{
        z-index: 2;
        display: none;
        text-align: left;
        width: 100%;
        font-size: 20px;
        line-height: 140%;
        padding-top: 10px;
        background: #08C;
        padding: 15px;
        color: white;
        position: absolute;
        top: 53px;
        left: 0;
        box-sizing: border-box;
        -webkit-animation-duration: 0.5s;
        -o-animation-duration: 0.5s;
        -moz-animation-duration: 0.5s;
        animation-duration: 0.5s;
      }

答案 1 :(得分:0)

您可以尝试编写以下代码:

HTML:

<div id="header"> 

<h1><center>Tabs</center></h1>
<ul>
    <li><a href="#" id="tab1">This</a></li>
    <li class="selected"><a href="#" id="tab2">That</a></li>
    <li><a href="#" id="tab3">The Other</a></li>
    <li><a href="#" id="tab4">Banana</a></li>
</ul>

</div>

<div id="content1" class="content">
    <p>Ispum schmipsum.11</p>
</div>
<div id="content2" class="content" style="display:none">
    <p>Ispum schmipsum.22</p>
</div>
<div id="content3" class="content" style="display:none">
    <p>Ispum schmipsum.33</p>
</div>
<div id="content4" class="content" style="display:none">
    <p>Ispum schmipsum.44</p>
</div>

CSS:

#header ul {
    list-style: none;
    padding:0;
    margin:0;
}


#header li {
    float: left;
    border: 1px solid;
    border-bottom-width: 0;
    margin: 0 0.5em 0 0;
}

#header li a {
     padding: 0 1em;
}

.content {
    border: 1px solid;
    clear: both;
}

h1 {
    margin: 0;
    padding: 0 0 1em 0;
} 



#header .selected {
    position: relative;
    top: 1px;
    background: white;
}

jQuery:

$("#tab1").click(function (){
    $(".content").hide();
    $("li").removeClass("selected");
    $("#content1").show();
    $(this).parent().addClass("selected");
});
$("#tab2").click(function (){
    $(".content").hide();
    $("li").removeClass("selected");
    $("#content2").show();
    $(this).parent().addClass("selected");
});
$("#tab3").click(function (){
    $(".content").hide();
    $("li").removeClass("selected");
    $("#content3").show();
    $(this).parent().addClass("selected");
});
$("#tab4").click(function (){
    $(".content").hide();
    $("li").removeClass("selected");
    $("#content4").show();
    $(this).parent().addClass("selected");
});

请参阅此处的演示:: http://jsfiddle.net/7d7gM/

答案 2 :(得分:0)

请参阅Fiddle here

$(document).ready(
    function() {
        $("ul#tabs > li").on("click", function() {
            $("ul#tabs > li").removeAttr("id");
            $(this).attr("id", "selected");
            if ($(this).find("a").text() == "This")
            {
                $("div#content > p").text("This content");
            }
            else if ($(this).find("a").text() == "That")
            {
                $("div#content > p").text("That content");
            }
            else if ($(this).find("a").text() == "The Other")
            {
                $("div#content > p").text("The Other content");
            }
            else if ($(this).find("a").text() == "Banana")
            {
                $("div#content > p").text("Banana content");
            }
        });

        //Trigger click on first tab to initiate
        $("ul#tabs > li:first-child").trigger("click");
    }
);