<!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>
<style>
*{
margin: 0 ;
padding: 0;
}
.syr{
width: 600px;
height: 400px;
border: 1px solid red;
margin: 100px auto;
}
.item{
width: 100%;
height: 40px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: space-around;
}
.item span{
border: 1px solid red;
padding: 10px 70px;
}
.itemlist{
width: 100%;
height: 356px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center;
}
.itemLists{
display: none;
}
.nones{
display: block;
}
</style>
</head>
<body>
<div class="syr">
<div class="item">
<span class="itemss nones">内容一</span>
<span class="itemss">内容二</span>
<span class="itemss">内容三</span>
</div>
<div class="itemlist">
<span class="itemLists nones">内容一</span>
<span class="itemLists">内容二</span>
<span class="itemLists">内容三</span>
</div>
</div>
<script>
var item = document.getElementsByClassName('itemss');
var itemLists = document.getElementsByClassName('itemLists');
// 添加类名法
for (let i = 0; i < item.length; i++) {
const element = item[i];
element.onclick = function () {
for (let k = 0; k < itemLists.length; k++) {
if (i == k) {
itemLists[k].classList.add('nones'); //添加带有display block的类名
}else{
itemLists[k].classList.remove('nones');//删除带有display block的类名
}
}
}
}
// display 法
// for (let i = 0; i < item.length; i++) {
// const element = item[i];
// element.onclick = function () {
// console.log(i);
// for (let k = 0; k < itemLists.length; k++) {
// if (i == k) {
// itemLists[k].style.display = 'block'
// }else{
// itemLists[k].style.display = 'none'
// }
// }
// }
// }
</script>
</body>
</html>
简单的原生JS tap栏切换
最新推荐文章于 2024-07-03 09:36:39 发布