html中为div添加展开与收起功能2(div隐藏与显示)


效果图:


1、单个隐藏div项 html布局

html 复制代码
<div class="question-detail active">
    <div class="item-handle">
        <span class="btn-detail">
            作答详情 <i class="layui-icon layui-icon-down layui-font-12"></i>
        </span>
    </div>
    <div class="item-answer">
        <div class="item-answer-content">

        </div>
    </div>
</div>

2、style样式

html 复制代码
<style type="text/css">

    .item-handle {
        height: 40px;
    }

    .question-detail .item-handle .btn-detail {
        -webkit-font-smoothing: antialiased;
        -webkit-box-orient: horizontal;
        -webkit-box-direction: normal;
        flex-direction: row;
        -webkit-box-align: center;
        align-items: center;
        -webkit-box-pack: center;
        justify-content: center;
        vertical-align: middle;
        position: relative;
        text-decoration: none;
        display: inline-block;
        line-height: 1;
        white-space: nowrap;
        background: #fff;
        -webkit-appearance: none;
        box-sizing: border-box;
        outline: none;
        margin: 0;
        transition: .1s;
        font-weight: 500;
        -webkit-user-select: none;
        float: right;
        width: 130px;
        height: 40.5px;
        text-align: center;
        color: #3c7af7;
        border: 1px solid #3c7af7;
        cursor: pointer;
        padding: 12px 20px;
        font-size: 14px;
        border-color: #fff;
        border-radius: 5px;
    }

    .question-detail.active .item-handle .btn-detail {
        float: right;
        width: 130px;
        height: 40.5px;
        text-align: center;
        color: #3c7af7;
        border: 1px solid #3c7af7;
        border-bottom-color: #fff;
        border-radius: 5px 5px 0 0;
        cursor: pointer;
        padding: 12px 20px;
        font-size: 14px;
    }

    .question-detail .item-answer {
        margin-top: -1px;
        border: 1px solid #3c7af7;
        padding: 15px 20px;
        display: none;
    }

    .question-detail.active .item-answer {
        display: block;
    }

    .item-answer-content {
        font-family: Helvetica Neue,Helvetica,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Arial,sans-serif;
        -webkit-font-smoothing: antialiased;
        color: #2c3e50;
        font-size: 14px;
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        min-height: 100px;
    }

</style>

3、js控制代码

javascript 复制代码
/** 点击作答详情, 通过修改css样式,控制item-answer对应div的显示与隐藏 */
$(document).on("click", ".item-handle .btn-detail", function () {
    var detailElem = $(this).parents(".question-detail");
    var isActive = detailElem.hasClass("active")

    if (isActive) detailElem.removeClass("active");
    else {
        detailElem.addClass("active");
    }
});
相关推荐
web小白成长日记18 分钟前
自定义 Hooks 的用法和意义详解(结合案例)
前端·css·面试·职场和发展·前端框架
小鸡脚来咯28 分钟前
前端传输的数据格式的选择
java·开发语言·前端·后端
小二·34 分钟前
【万字源码级剖析】深入理解 Vue 3 响应式系统:ref、reactive、computed 与 effect 的底层实现
前端·javascript·vue.js
Mintopia40 分钟前
“开源”和“闭源“,AI 模型的发展方向
前端·人工智能·aigc
Mintopia42 分钟前
哈珀·李的《**杀死一只知更鸟**》(*To Kill a Mockingbird*)是一部关于**人性、正义与道德成长**的小说
前端
且菜且折腾1 小时前
react快捷键hook
javascript·react.js·ecmascript
什么都不会的Tristan1 小时前
Feed流(关注推送)
java·前端·数据库
IT_陈寒1 小时前
Vite 5.0 性能优化实战:从3秒到300ms的构建提速秘籍
前端·人工智能·后端
一路向前的月光1 小时前
前端采用electron-hiprint控件实现静默打印
前端·javascript·electron
Jagger_1 小时前
AI还原设计稿方法
前端