Wie man erstellt: Tabellenkopftitel
- Vorherige Seite Vertikale Registerkarten
- Nächste Seite Volle Seite Registerkarten
Lernen Sie, wie man Tabellenkopftitel mit CSS und JavaScript erstellt.
Tabellenkopftitel
Klicken Sie auf die Schaltfläche "city", um den entsprechenden Titel anzuzeigen:
London
London ist die Hauptstadt der Stadt England.
Paris
Paris ist die Hauptstadt von Frankreich.
Tokyo
Tokyo ist die Hauptstadt von Japan.
Oslo
Oslo ist die Hauptstadt von Norwegen.
Erstellen Sie umschaltbare Tabellenkopftitel
Schritt 1 - Fügen Sie HTML hinzu:
<div id="London" class="tabcontent"> <h1>London</h1> <p>London ist die Hauptstadt von England.</p> </div> <div id="Paris" class="tabcontent"> <h1>Paris</h1> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"> <h1>Tokyo</h1> <p>Tokyo is the capital of Japan.</p> </div> <div id="Oslo" class="tabcontent"> <h1>Oslo</h1> <p>Oslo is the capital of Norway.</p> </div> <button class="tablink" onclick="openCity('London', this, 'red')" id="defaultOpen">London</button> <button class="tablink" onclick="openCity('Paris', this, 'green')">Paris</button> <button class="tablink" onclick="openCity('Tokyo', this, 'blue')">Tokyo</button> <button class="tablink" onclick="openCity('Oslo', this, 'orange')">Oslo</button> Create buttons to open specific tab content. All <div> elements with class="tabcontent" are hidden by default (with CSS & JS). When the user clicks on a button - it will open the tab content that "matches" this button.
Step 2 - Add CSS:
Set the style of the button and the tab content:
/* Set the style of the tab button */ .tablink { background-color: #555; color: white; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; font-size: 17px; width: 25%; } /* 更改悬停时按钮的背景颜色 */ .tablink:hover { background-color: #777; } /* 设置标签页内容的默认样式 */ .tabcontent { color: white; display: none; padding: 50px; text-align: center; } /* 单独设置每个标签页内容的样式 */ #London {background-color:red;} #Paris {background-color:green;} #Tokyo {background-color:blue;} #Oslo {background-color:orange;}
Schritt 3 - Fügen Sie JavaScript hinzu:
function openCity(cityName, elmnt, color) { // 默认隐藏所有 class="tabcontent" 的元素 */ var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // 删除所有标签页链接/按钮的背景颜色 tablinks = document.getElementsByClassName("tablink"); for (i = 0; i < tablinks.length; i++) { tablinks[i].style.backgroundColor = ""; } // 显示具体标签页内容 document.getElementById(cityName).style.display = "block"; // 为用于打开标签页内容的按钮添加特定的颜色 elmnt.style.backgroundColor = color; } // 获取 id="defaultOpen" 的元素并点击它 document.getElementById("defaultOpen").click();
Verwandte Seiten
Anleitung:Wie kann man Reiter erstellen
- Vorherige Seite Vertikale Registerkarten
- Nächste Seite Volle Seite Registerkarten