无法在html中创建选项卡式面板

时间:2014-08-05 05:58:19

标签: javascript html css

我希望创建一个带有三个标签的标签面板。 下面的HTML代码和脚本:

<span id="tab1">
    <a href="#" onClick="showTabOne();"> Tab-1  |</a> 

</span>
<span id="tab2"><a href="#" onClick="showTabTwo();"> Tab-2  |</a> </span>
<span id="tab3"><a href="#" onClick="showTabThree();"> Tab-3  |</a> </span>
</div>



<script type="text/javascript">
    function showTabOne() {
        document.getElementById("tab1").innerHTML = "yayy ! this is tab-1";
    }

    function showTabTwo() {
        document.getElementById("tab2").innerHTML = "and this is tab-2";
    }

    function showTabThree() {
        document.getElementById("tab3").innerHTML = "and..tab-3";
    }
</script>

当我点击tab1时,它会显示其内容,但tab1标题会丢失,而其他两个标签也是如此。

4 个答案:

答案 0 :(得分:1)

通常,您需要在标签链接(标题)之后设置<span id = "tabContent"></span>

HTML代码如下所示:

<span id="tab1">
<a href="#" onClick="showTabOne();"> Tab-1  |</a> 

</span>
<span id="tab2"><a href="#" onClick="showTabTwo();"> Tab-2  |</a> </span>
<span id="tab3"><a href="#" onClick="showTabThree();"> Tab-3  |</a> </span>
<span id = "tabContent"></span> <!-- newly added line -->
</div>

而且,代码应该改为:

function showTabOne() {
    document.getElementById("tabContent").innerHTML = "yayy ! this is tab-1";
}

function showTabTwo() {
    document.getElementById("tabContent").innerHTML = "yayy ! this is tab-2";
}

function showTabThree() {
    document.getElementById("tabContent").innerHTML = "yayy ! this is tab-3";
}

您的代码问题在于您正在使用选项卡内容替换tab1链接(标题)。

答案 1 :(得分:0)

使用此

document.getElementById("tab1").innerHTML = document.getElementById("tab1").innerHTML + "yayy ! this is tab-1";

答案 2 :(得分:0)

我认为你应该选择这样的东西..

<span ><a href="#" onClick="showTab(tab1);"> Tab-1  |</a> </span>
<span ><a href="#" onClick="showTab(tab2);"> Tab-2  |</a> </span>
<span ><a href="#" onClick="showTab(tab3);"> Tab-3  |</a> </span>


<div id="tab1" class="tabContents">   <!-- tabContents by default hidden-->
  tab 1 content
</div>
<div id="tab2" class="tabContents">
  tab 2 content
</div>
<div id="tab3" class="tabContents">
   tab 3 content
</div>


<script type="text/javascript">
    function showTab(tb) {
        var elements = getElementsByClassName('tabContents');
           for(i in elements ){
             elements[i].style.display = "none";
                }
        document.getElementById(tb).style.display = "block";
    }
</script>

答案 3 :(得分:0)

这很有效。

HTML     

</div>
<div class="tabs">
    <!-- Radio button and lable for #tab-content1 -->
    <input type="radio" name="tabs" id="tab1" checked >
    <label for="tab1">
       <i class="fa fa-rocket" aria-hidden="true"></i>
<span>Projects</span>
    </label>
    <!-- Radio button and lable for #tab-content2 -->
    <input type="radio" name="tabs" id="tab2">
    <label for="tab2">
        <i class="fa fa-users" aria-hidden="true"></i><span>Flash-Mobs</span>
    </label>
    <!-- Radio button and lable for #tab-content3 -->
    <input type="radio" name="tabs" id="tab3">
    <label for="tab3">
       <i class="fa fa-heartbeat" aria-hidden="true"></i><span>Movement</span>
    </label>
    <div id="tab-content1" class="tab-content">
        <h3>Positive Action Projects</h3>
        <p><!-- Tab content here --></p>
    </div> <!-- #tab-content1 -->
    <div id="tab-content2" class="tab-content">
        <h3>Internatonal Positive Action Days</h3>
        <p><!-- Tab content here --></p>
    </div> <!-- #tab-content2 -->
    <div id="tab-content3" class="tab-content">
     <h3>Grow the Movement</h3>
        <p><!-- Tab content here --></p>
    </div> <!-- #tab-content2 -->
    </div>

CSS

    .tabs {
    max-width: 90%;
    float: none;
    list-style: none;
    padding: 0;
    margin: 75px auto;
    border-bottom: 4px solid #ccc;
}
.tabs:after {
    content: '';
    display: table;
    clear: both;
}
.tabs input[type=radio] {
    display:none;
}
.tabs label {
    display: block;
    float: left;
    width: 33.3333%;
    color: #ccc;
    font-size: 30px;
    font-weight: normal;
    text-decoration: none;
    text-align: center;
    line-height: 2;
    cursor: pointer;
    box-shadow: inset 0 4px #ccc;
    border-bottom: 4px solid #ccc;
    -webkit-transition: all 0.5s; /* Safari 3.1 to 6.0 */
    transition: all 0.5s;
}
.tabs label span {
    display: none;
}
.tabs label i {
    padding: 5px;
    margin-right: 0;
}
.tabs label:hover {
    color: #3498db;
    box-shadow: inset 0 4px #3498db;
    border-bottom: 4px solid #3498db;
}
.tab-content {
    display: none;
    width: 100%;
    float: left;
    padding: 15px;
    box-sizing: border-box;
    background-color:#ffffff;
}

.tab-content * {
    -webkit-animation: scale 0.7s ease-in-out;
    -moz-animation: scale 0.7s ease-in-out;
    animation: scale 0.7s ease-in-out;
}
@keyframes scale {
  0% {
    transform: scale(0.9);
    opacity: 0;
    }
  50% {
    transform: scale(1.01);
    opacity: 0.5;
    }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.tabs [id^="tab"]:checked + label {
    background: #FFF;
    box-shadow: inset 0 4px #3498db;
    border-bottom: 4px solid #3498db;
    color: #3498db;
}
#tab1:checked ~ #tab-content1,
#tab2:checked ~ #tab-content2,
#tab3:checked ~ #tab-content3 {
    display: block;
}

@media (min-width: 768px) {
    .tabs i {
        padding: 5px;
        margin-right: 10px;
    }
    .tabs label span {
        display: inline-block;
    }
    .tabs {
    max-width: 750px;
    margin: 50px auto;
    }
}