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");
    }
});
相关推荐
下一站丶11 小时前
【JavaScript性能优化实战】
开发语言·javascript·性能优化
GIS好难学11 小时前
Three.js 粒子特效实战③:粒子重组效果
开发语言·前端·javascript
申阳11 小时前
Day 2:我用了2小时,上线了一个还算凑合的博客站点
前端·后端·程序员
刺客_Andy12 小时前
React 第四十七节 Router 中useLinkClickHandler使用详解及开发注意事项案例
前端·javascript·react.js
爱分享的鱼鱼12 小时前
Java实践之路(一):记账程序
前端·后端
爱编码的傅同学12 小时前
【HTML教学】成为前端大师的入门教学
前端·html
爱看书的小沐12 小时前
【小沐杂货铺】基于Three.js绘制三维管道Pipe(WebGL、vue、react)
javascript·vue.js·webgl·three.js·管道·pipe·三维管道
w2sfot12 小时前
如何将React自定义语法转化为标准JavaScript语法?
javascript·react
秋枫9612 小时前
使用React Bootstrap搭建网页界面
前端·react.js·bootstrap
不一样的少年_12 小时前
上班摸鱼看掘金,老板突然出现在身后...
前端·javascript·浏览器