用html写一个标签页
时间: 2023-05-19 12:07:22 浏览: 111
html标签
可以使用以下代码来创建一个基本的标签页:
<!DOCTYPE html>
<html>
<head>
<title>标签页示例</title>
<style>
/* 标签页样式 */
.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.active {
background-color: #ccc;
}
/* 标签内容样式 */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
</style>
</head>
<body>
<!-- 标签页 -->
<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">
<h3>标签1</h3>
<p>这是标签1的内容。</p>
</div>
<div id="tab2" class="tabcontent">
<h3>标签2</h3>
<p>这是标签2的内容。</p>
</div>
<div id="tab3" class="tabcontent">
<h3>标签3</h3>
<p>这是标签3的内容。</p>
</div>
<script>
// 打开指定标签页
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";
}
</script>
</body>
</html>
阅读全文