Files
ClassContent/历届学生/front-end-team/项目练习/学生项目/陆旗/笔记.html/tabsv2.html
2024-09-27 02:06:13 +08:00

130 lines
2.8 KiB
HTML

<!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>
</head>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
ul {
display: flex;
}
.tabs_title {
height: 45px;
align-items: center;
}
.tabs_title li {
margin: 0 10px;
}
.tabs_content li {
display: none;
}
.active_content {
display: block !important;
}
.active_title {
color: red;
}
</style>
<body>
<ul class="tabs_title"></ul>
<ul class="tabs_content"> </ul>
</body>
<script>
var index = 0
var tabsData = [
{
id: 1,
title: '首页',
page: 1,
content: [
{ Text: '首页的内容1' },
{ Text: '首页的内容1' },
{ Text: '首页的内容1' },
]
},
{
id: 1,
title: '首页',
page: 1,
content: [
{ Text: '首页的内容1' },
{ Text: '首页的内容1' },
{ Text: '首页的内容1' },
]
},
{
id: 1,
title: '首页',
page: 1,
content: [
{ Text: '首页的内容1' },
{ Text: '首页的内容1' },
{ Text: '首页的内容1' },
]
},
];
function renderTitle() {
tabsData.forEach((v, i) => {
$('.tabs_title').innerHTML += `<li class="${i == 0 ? 'active_title' : ""}">${v.title}</li>`;
$('.tabs_content').innerHTML += `<li class="${i == 0 ? 'active_content' : ""}">${v.title}</li>`;
})
_('tabs_title li').forEach((v, i) => {
v.onclick = () => {
index = i;
_('.tabs_title li').forEach((j, index) => {
j.classList.remove('active_title')
_('.tabs_content li')[index].classList.remove('active_content');
})
v.classList.add('active_title')
_('.tabs_content li')[index].classList.add('active_content');
renderContent();
}
})
}
function renderContent() {
_(".tabs_content li")[index].innerHTML = ""
tabsData[index].content.forEach((v) => {
_('.tabs_content li')[index].innerHTML += `<p>${v.text}</p>`
})
}
renderTitle();
renderContent();
function _(className) {
return document.querySelectorAll(className)
}
function $(className) {
return document.querySelector(className)
}
</script>
</html>