<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
font-family: Arial;
}
.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;
font-size: 17px;
}
.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;
}
.tabcontent.active {
display: block;
}
</style>
</head>
<body>
<div class="tab">
<button class="tablinks" data-city="London">London</button>
<button class="tablinks" data-city="Paris">Paris</button>
<button class="tablinks" data-city="Tokyo">Tokyo</button>
</div>
<div id="London" class="tabcontent active">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div id="Paris" class="tabcontent ">
<h3>Paris</h3>
<p>Paris is the capital city of France.</p>
</div>
<div id="Tokyo" class="tabcontent ">
<h3>Tokyo</h3>
<p>Tokyo is the capital city of Japan.</p>
</div>
<script>
const tabLinks = document.querySelectorAll(".tablinks");
for (const tab of tabLinks) {
tab.addEventListener('click', e => {
for (const item of document.querySelectorAll(".tablinks")) {
item.classList.remove('active');
}
e.currentTarget.classList.add('active');
for (const txt of document.querySelectorAll(".tabcontent")) {
txt.classList.remove('active');
}
const city = e.currentTarget.dataset.city;
document.getElementById(city).classList.add('active');
});
}
</script>
</body>
</html>