2025-04-10

作业描述:使用JS实现一个选项卡效果

网页代码

使用Trae CN工具编写代码,如下:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    html,body{
        height: 100%;
        width: 100%;
    }
    html{
        font-size: 10px;
    }
    .tab{
        width: 40rem;
        height: 30rem;
        border:solid 1px #000 ;
    }
    .tab-title{
        width: 100%;
        height:5 rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #ccc;
        color:#fff;
    }
    .tt{
        flex:1;
        height: 5rem;
        background-color: rgba(123, 232, 176, 0.7);
        text-align: center;
        line-height: 5rem;
        font-size: 1.8rem;
    }
    .tt.active,
    .tt:hover{
        background-color: rgb(243, 223, 188);
        color: blueviolet;
    }
    .table-content{
        width: 100%;
        height: 25rem;
        background-color: #e87676;
        position: relative;
    }
    .tc{
        width: 100%;
        height: 25rem;
        text-align: center;
        background-color: #c03535;
        font-size: 1.8rem;
        display: none;
        position: absolute;
    }
   .tc.active{
        display: block;
    }
    .tc:nth-of-type(1){
        background-color: rgb(34, 107, 253);
    }
    .tc:nth-of-type(2){
        background-color: darkorange;
    }
    .tc:nth-of-type(3){
        background-color: darkslateblue;
    }


</style>
<body>
    <div class="tab">
        <div class="tab-title">
            <div class="tt active">标题1</div>
            <div class="tt">标题2</div>
            <div class="tt">标题3</div>
        </div>
        <div class="table-content">
            <div class="tc active">内容1</div>
            <div class="tc">内容2</div>
            <div class="tc">内容3</div>
        </div>
    </div>
    <script>
        let tts=document.getElementsByClassName("tt")
        console.log(tts)
        for(let i=0;i<tts.length;i++){
            console.log(tts[i]);
            tts[i].onmouseenter=function(){
                for(let j=0;j<tts.length;j++){
                    tts[j].classList="tt"
                }
                tts[i].classList="tt active"

                let tcs=document.getElementsByClassName("tc")
                for(let k=0;k<tcs.length;k++){
                    tcs[k].classList="tc"
                }
                tcs[i].classList="tc active"
            }
        }
        
    </script>
</body>
</html>
屏幕截图 2025-04-10 173740.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 同城游戏开发干货精选从入门到进阶的实用指南 游戏开发是上备受关注的技...
    糖诗诗阅读 24评论 0 0
  • 《现代软件开发中的“技术债”:如何平衡速度与代码质量?》...
    糖诗诗阅读 22评论 0 0
  • 新版本默认会使用默认的JDK版本,老项目可能不支持,需要单独设置https://stackoverflow.com...
    对方正在杰收阅读 33评论 0 0
  • 一、行业背景 在制造业蓬勃发展的当下,产品生产的精细化与高效化需求日益凸显。自动锁螺丝机作为提升螺丝装配效率与质量...
    数能阅读 45评论 0 3
  • 一、考试介绍 1.1 背景介绍 计算机软件资格考试是由国家人力资源和社会保障部、工业和信息化部领导下的国家级考试。...
    Cherish918阅读 21评论 0 0