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");
    }
});
相关推荐
snow@li8 分钟前
Java:Java 服务器(Web 容器 / Servlet 容器)完整工作清单
java·服务器·前端
用户440145493141939 分钟前
Spring Boot 3.x 异步编程实战:从 @Async 到虚拟线程
javascript
洋子1 小时前
Yank Note 系列 14 - 我和 AI 怎么一起写文章
前端·markdown
江华森1 小时前
前端构建工具 Webpack 速览
前端·webpack·node.js
谙忆10242 小时前
HTTP 图片缓存实战:Cache-Control、ETag、内容哈希文件名与 CDN 回源到底怎么配
前端
wangruofeng2 小时前
终端里 rm 一下文件就没了,macOS 怎么删文件才进废纸篓
javascript·命令行·apple
小妖6662 小时前
React input 输入卡顿问题
前端·javascript·react.js
এ慕ོ冬℘゜2 小时前
JavaScript 数据类型检测与转换、运算符超全实战详解
前端·html
kyriewen2 小时前
我用 AI 写代码快了 3 倍——然后同事的 Code Review 全变成了我的
前端·程序员·ai编程
李明卫杭州3 小时前
Vue2 portal-target 组件与 Vue3 的针对性优化
前端·javascript·vue.js