방법 - 탭 헤더
CSS와 JavaScript로 탭 헤더를 만드는 방법을 알아보세요.
탭 헤더
적절한 헤더를 표시하려면 "도시" 버튼을 클릭하십시오.
도쿄
도쿄는 일본의 수도입니다.
전환 가능한 탭 헤더 만들기
1단계) HTML 추가:
예시
<div id="London" class="tabcontent">
<h1>London</h1>
<p>London is the
capital city of 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>
특정 탭 콘텐츠를 여는 버튼을 만듭니다. 모든 <div> 요소 class="tabcontent"
는 기본적으로 숨겨져 있습니다(CSS 및 JS 사용). 사용자가 버튼을 클릭하면 이 버튼과 "일치하는" 탭 콘텐츠가 열립니다.
2단계) CSS 추가:
버튼 및 탭 콘텐츠 스타일 지정:
예시
/* Style the tab buttons */
.tablink {
background-color: #555;
color: white;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
font-size: 17px;
width: 25%;
}
/* Change background color of buttons on hover */
.tablink:hover {
background-color: #777;
}
/* Set default styles for tab content */
.tabcontent
{
color: white;
display: none;
padding: 50px;
text-align: center;
}
/* Style
each tab content individually */
#London {background-color:red;}
#Paris {background-color:green;}
#Tokyo {background-color:blue;}
#Oslo {background-color:orange;}
3단계) 자바스크립트 추가:
예시
function openCity(cityName, elmnt, color) {
// Hide all
elements with class="tabcontent" by default */
var i,
tabcontent, tablinks;
tabcontent =
document.getElementsByClassName("tabcontent");
for (i =
0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Remove the background color of all tablinks/buttons
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = "";
}
// Show the specific tab content
document.getElementById(cityName).style.display = "block";
// Add the specific color to the button used to open the tab content
elmnt.style.backgroundColor = color;
}
// Get the element with id="defaultOpen"
and click on it
document.getElementById("defaultOpen").click();
팁: 방법 - 탭 도 확인하세요 .