158 lines
3.8 KiB
HTML
158 lines
3.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>tabs切换</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
list-style: none;
|
|
}
|
|
|
|
.title {
|
|
display: flex;
|
|
}
|
|
|
|
.title li {
|
|
margin-right: 20px;
|
|
}
|
|
|
|
.title .active {
|
|
color: red;
|
|
}
|
|
|
|
.content li {
|
|
display: none;
|
|
}
|
|
|
|
.content .active {
|
|
display: block;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<ul class="title">
|
|
|
|
</ul>
|
|
|
|
<ul class="content">
|
|
|
|
</ul>
|
|
</body>
|
|
<script>
|
|
var tabsList = [
|
|
{
|
|
title: '社会',
|
|
content: [
|
|
{
|
|
title: '社会新闻',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
title: '娱乐',
|
|
content: [
|
|
{
|
|
title: '娱乐新闻1',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
},
|
|
{
|
|
title: '娱乐新闻2',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
title: '体育',
|
|
content: [
|
|
{
|
|
title: '娱乐新闻1',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
},
|
|
{
|
|
title: '体育新闻2',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
},
|
|
{
|
|
title: '体育新闻3',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
title: '军事',
|
|
content: [
|
|
{
|
|
title: '娱乐新闻1',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
},
|
|
{
|
|
title: '军事新闻2',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
},
|
|
{
|
|
title: '军事新闻3',
|
|
time: '2023',
|
|
text: '主题内容部分'
|
|
}
|
|
]
|
|
}
|
|
]
|
|
|
|
var title = $(".title"),
|
|
content = $(".content");
|
|
function renderTitle() {
|
|
tabsList.forEach((v, i) => {
|
|
// console.log(v)
|
|
//向title中添加li
|
|
title.innerHTML += `<li class="${i == 0 ? 'active' : ''}">${v.title}</li>`
|
|
content.innerHTML += `<li class="${i == 0 ? 'active' : ''}">
|
|
${
|
|
v.content.length == 0 ? `<p>暂无数据</p>` : v.content.map(v => {
|
|
return `<p>${v.title}</p>`//p中间显示title部分内容
|
|
|
|
}).toString().replaceAll(",", "")
|
|
}
|
|
</li>`
|
|
})
|
|
bindClick()
|
|
}
|
|
renderTitle()
|
|
function bindClick() {
|
|
_(".title li").forEach((v, i) => {
|
|
v.onclick = () => {
|
|
_(".title li").forEach((val,index) => {
|
|
val.className = ""
|
|
_(".content li")[index].className = ""
|
|
})
|
|
v.className = "active"
|
|
_(".content li")[i].className = "active"
|
|
}
|
|
})
|
|
}
|
|
|
|
function $(className) {
|
|
return document.querySelector(className)
|
|
}
|
|
|
|
function _(className) {
|
|
return document.querySelectorAll(className)
|
|
}
|
|
</script>
|
|
|
|
</html> |