标签栏切换

<div class="tab-head">
							<ul>
								<li class="tab-head1 current"><a href="">猜你喜欢</a></li>
								<li class="tab-head1"><a href="">猜你喜欢</a></li>
								<li class="tab-head1"><a href="">猜你喜欢</a></li>
								<li class="tab-head1"><a href="">猜你喜欢</a></li>
							</ul>
						</div>
						<div class="tab-body">
							<div class="tab-body1 current1">111</div>
							<div class="tab-body1">222</div>
							<div class="tab-body1">333</div>
							<div class="tab-body1">444</div>
						</div>
					</div>
.tab{
	position: relative;
	width: 100%;
	height: 290px;
	margin-bottom: 10px;
}
.tab-left{
	width: 70%;
	height: 290px;
	border:1px solid #EEEEEE;
	float: left;
}
.tab-right{
	width: 28%;
	height: 290px;
	border:1px solid #EEEEEE;
	float:left;
	margin-left: 10px;
}
.tab-head{
	width: 100%;
	height: 40px;
	background-color: #EFEFEF;
	border-top:3px solid #FF0000;
}
.tab-head li{
	border-left: 1px solid lightgray;
	font-size: 20px;
	height: 38px;
	float: left;
	width: 24%;
}
.tab-head li a{
	line-height: 40px;
	margin-left: 24px;
}
.tab-head ul .current{
	background-color: #D3D3D3;
}
.tab-body{
	position:relative;
}
.tab-body div{
	position: absolute;
	width: 100%;
	height: 258px;
	background-color: red;
	display: none;
}
.tab-body .current1{
	display: block;
}
var tabs=document.getElementsByClassName("tab-head1");
var divs=document.getElementsByClassName("tab-body1");
for(var i=0;i<tabs.length;i++){
	tabs[i].onmouseover=function(){
		for(var	j=0;j<divs.length;j++){
			//和tabs[i]并没有关系,可以理解为就近原则
			if(tabs[j]==this){
				tabs[j].classList.add("current");
				divs[j].classList.add("current1");
			}else{
				tabs[j].classList.remove("current");
				divs[j].classList.remove("current1");
			}
		}
	}
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的标签切换的实现: HTML 代码: ```html <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">标签1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">标签2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">标签3</button> </div> <div id="tab1" class="tabcontent"> <h3>标签1</h3> <p>这是标签1的内容。</p> </div> <div id="tab2" class="tabcontent"> <h3>标签2</h3> <p>这是标签2的内容。</p> </div> <div id="tab3" class="tabcontent"> <h3>标签3</h3> <p>这是标签3的内容。</p> </div> ``` CSS 代码: ```css /* 隐藏所有内容 */ .tabcontent { display: none; } /* 激活的标签按钮 */ .tab button.active { background-color: #ccc; } /* 标签样式 */ .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } /* 标签按钮样式 */ .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } /* 鼠标悬停时的样式 */ .tab button:hover { background-color: #ddd; } /* 激活的标签按钮样式 */ .tab button.active { background-color: #ccc; } /* 标签内容样式 */ .tabcontent { padding: 6px 12px; border: 1px solid #ccc; border-top: none; } ``` JavaScript 代码: ```javascript function openTab(evt, tabName) { // 隐藏所有标签内容 var tabcontent = document.getElementsByClassName("tabcontent"); for (var i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // 移除所有激活的标签按钮 var tablinks = document.getElementsByClassName("tablinks"); for (var i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } // 显示当前标签内容并激活当前标签按钮 document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } ``` 在这个例子中,我们使用了三个标签按钮和三个标签内容,每个标签按钮都有一个对应的 ID,当点击标签按钮时,通过 ID 找到对应的标签内容进行切换。通过添加和移除 CSS 类名来改变样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值