84 lines
1.8 KiB
HTML
84 lines
1.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>js_案例tabs</title>
|
||
<style>
|
||
* {
|
||
margin: 0;
|
||
padding: 0;
|
||
list-style: none;
|
||
}
|
||
|
||
.title {
|
||
height: 60px;
|
||
display: flex;
|
||
}
|
||
|
||
.title li {
|
||
width: 80px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
|
||
.text {
|
||
height: 40px;
|
||
}
|
||
|
||
.text li {
|
||
width: 320px;
|
||
height: 40px;
|
||
display: none;
|
||
}
|
||
.text .active1{
|
||
display: block;
|
||
}
|
||
.active {
|
||
color: #fff;
|
||
background: red;
|
||
}
|
||
.active1{
|
||
color: black;
|
||
}
|
||
</style>
|
||
</head>
|
||
|
||
<body>
|
||
<ul class="title">
|
||
<li class="active">社会</li>
|
||
<li>娱乐</li>
|
||
<li>体育</li>
|
||
<li>军事</li>
|
||
</ul>
|
||
<ul class="text">
|
||
<li class="active1">社会新闻</li>
|
||
<li>娱乐新闻</li>
|
||
<li>体育新闻</li>
|
||
<li>军事新闻</li>
|
||
</ul>
|
||
</body>
|
||
<script>
|
||
//循环li标签,并绑定点击
|
||
_(".title li").forEach((v,i) => {
|
||
v.onclick = () =>{
|
||
_(".title li").forEach((val,index) => {
|
||
val.className = ""
|
||
_(".text li")[index].className = ""
|
||
})
|
||
v.className = "active"
|
||
_(".text li")[i].className = "active1"
|
||
}
|
||
})
|
||
function $(className) {
|
||
return document.querySelector(className);
|
||
}
|
||
function _(className) {
|
||
return document.querySelectorAll(className);
|
||
}
|
||
</script>
|
||
|
||
</html> |