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");
    }
});
相关推荐
涔溪27 分钟前
vue2+Three.js或WebGL上传预览CAD文件
javascript·vue.js·webgl
专注VB编程开发20年33 分钟前
如何保存网站CSS和JS中的图片?网页另存为本地显示不正常
前端·javascript·css
丶重明37 分钟前
【2024】前端学习笔记9-内部样式表-外部导入样式表-类选择器
前端·笔记·学习
又写了一天BUG1 小时前
关于在vue2中给el-input等输入框的placeholder加样式
前端·javascript·vue.js
秋月霜风1 小时前
Web爬虫应用功能及需求设计
前端·爬虫·python
蜡笔小新星1 小时前
前端框架对比和选择
前端·javascript·vue.js·经验分享·学习·前端框架
LE...1 小时前
HTML元素居中
css·html·css3
一直在学习的小白~2 小时前
中间添加一条可以拖拽的分界线,来动态调整两个模块的宽度
前端·javascript·react.js
此白非彼白`2 小时前
vue使用PDF.JS踩的坑--部署到服务器上显示pdf.mjs viewer.mjs找不到资源
javascript·vue.js·pdf·asp.net
&木头人&2 小时前
layui upload.render 设置文件名
前端·javascript·layui