标签切换的css方法实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码实现tab标签切换: 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"> <p>这是标签1的内容。</p> </div> <div id="tab2" class="tabcontent"> <p>这是标签2的内容。</p> </div> <div id="tab3" class="tabcontent"> <p>这是标签3的内容。</p> </div> ``` CSS代码: ```css .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 { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } .tabcontent.show { display: block; } ``` JavaScript代码: ```javascript function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].classList.remove("show"); } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].classList.remove("active"); } document.getElementById(tabName).classList.add("show"); evt.currentTarget.classList.add("active"); } ``` 在JavaScript代码中,openTab()函数会根据传入的参数tabName来显示相应的标签页,并将对应的按钮设为激活状态。同时,如果有其他标签页处于激活状态,会将其隐藏并将对应的按钮设为非激活状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值