以下是利用JS实现简单选项卡功能的示例代码:
HTML代码:
<div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">选项卡1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">选项卡2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">选项卡3</button> </div> <div id="tab1" class="tabcontent"> <h2>选项卡1</h2> <p>这是选项卡1的内容。</p> </div> <div id="tab2" class="tabcontent"> <h2>选项卡2</h2> <p>这是选项卡2的内容。</p> </div> <div id="tab3" class="tabcontent"> <h2>选项卡3</h2> <p>这是选项卡3的内容。</p> </div>
CSS代码:
.tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }
JS代码:
function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; }
在以上示例中,通过定义 openTab 函数,实现了选项卡的切换功能。在点击选项卡按钮时,该函数会隐藏所有选项卡的内容,并将选项卡按钮的样式设置为未激活状态。然后,根据指定的选项卡名称显示对应的选项卡内容,并将该选项卡按钮的样式设置为激活状态。
以下是利用JS实现简单选项卡功能的示例代码:
HTML代码:
<div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">选项卡1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">选项卡2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">选项卡3</button> </div> <div id="tab1" class="tabcontent"> <h2>选项卡1</h2> <p>这是选项卡1的内容。</p> </div> <div id="tab2" class="tabcontent"> <h2>选项卡2</h2> <p>这是选项卡2的内容。</p> </div> <div id="tab3" class="tabcontent"> <h2>选项卡3</h2> <p>这是选项卡3的内容。</p> </div>
CSS代码:
.tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }
JS代码:
function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; }
在以上示例中,通过定义 openTab 函数,实现了选项卡的切换功能。在点击选项卡按钮时,该函数会隐藏所有选项卡的内容,并将选项卡按钮的样式设置为未激活状态。然后,根据指定的选项卡名称显示对应的选项卡内容,并将该选项卡按钮的样式设置为激活状态。