用锚记和选项卡方式 做一个分类切换页面完整代码
时间: 2024-01-30 20:01:50 浏览: 26
好的,以下是一个简单的分类切换页面的完整代码,采用锚记和选项卡方式实现:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>分类切换页面</title>
<meta charset="UTF-8">
<style>
.tabcontent {
display: none;
}
</style>
</head>
<body>
<h1>分类切换页面</h1>
<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>
<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>
```
CSS样式代码:
```css
.tabcontent {
display: none;
}
.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 {
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
```
JavaScript代码:
```javascript
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()`函数来实现切换选项卡。当点击某个选项卡按钮时,该函数会将所有选项卡内容隐藏,将当前选项卡内容显示,并将当前选项卡按钮标记为活动状态。
同时,使用了CSS样式来美化选项卡和选项卡内容的样式。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)