纯HTML和CSS实现点击切换

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
    <style>
    img {
      display: none;
      width: 100px;
      height: 100px;
    }
    input:checked + img {
      display: block;
    }
    input {
      position: absolute;
      left: -9999px;
    }
    label {
      cursor: pointer;
    }
    </style>
  </head>
  <body>
    <div id="cont">
      <input id="img1" name="img" type="radio" checked="checked">
      <img src="./img1.png" style="background: red">
      <input id="img2" name="img" type="radio">
      <img src="./img2.png" style="background: black">
    </div>
    <div id="nav">
      <label for="img1">第一张</label>
      <label for="img2">第二张</label>
    </div>
  </body>
</html>

  

转载于:https://www.cnblogs.com/detanx/p/htmlCssCcheck.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用CSS和一点点JavaScript来实现页签切换效果。 HTML部分: ``` <div class="tab-container"> <div class="tab">Tab 1</div> <div class="tab">Tab 2</div> <div class="tab">Tab 3</div> <div class="tab-content active">This is tab 1 content.</div> <div class="tab-content">This is tab 2 content.</div> <div class="tab-content">This is tab 3 content.</div> </div> ``` CSS部分: ``` .tab-container { display: flex; flex-wrap: wrap; } .tab { padding: 10px 20px; background-color: #f0f0f0; border: 1px solid #ccc; cursor: pointer; margin-right: 10px; margin-bottom: 10px; } .tab.active { background-color: #ccc; } .tab-content { display: none; padding: 20px; } .tab-content.active { display: block; } ``` JavaScript部分: ``` const tabs = document.querySelectorAll('.tab'); const tabContents = document.querySelectorAll('.tab-content'); tabs.forEach(tab => { tab.addEventListener('click', () => { // 移除所有页签和内容的 active 类 tabs.forEach(tab => tab.classList.remove('active')); tabContents.forEach(content => content.classList.remove('active')); // 添加当前选中页签和内容的 active 类 tab.classList.add('active'); const targetContent = document.querySelector(`.${tab.dataset.target}`); targetContent.classList.add('active'); }); }); ``` 其中,`.tab` 元素中的 `data-target` 属性用来确定对应的 `.tab-content` 元素。 这样,当用户点击某个页签时,JavaScript代码会移除所有页签和内容的 `active` 类,然后给当前选中的页签和内容添加 `active` 类,从而实现页签切换效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值