Files
ClassContent/历届学生/front-end-team-10/项目练习/学生项目/韩孟雪/2023-2-25/js_案例3tabs.html
2024-09-27 02:06:13 +08:00

84 lines
1.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>