前端小案例——折叠面板(HTML+CSS+JS, 附源码)

本文详细描述了如何使用JavaScript和CSS创建一个可折叠的问题知识列表,包括监听h3标签的点击事件并控制对应div内容的显示/隐藏。同时提供了HTML和JavaScript代码示例,以及对可能出现的问题和解决方法的讨论。
摘要由CSDN通过智能技术生成

一、前言

实现功能:

        用于展示问题知识列表。当点击问题标题时,对应的答案内容将展开或折叠起来。

实现逻辑:

        设置标题(h3标签)和相应的答案(div标签),然后用JS监听h3标签的点击事件,并通过修改对应的div标签的display属性来实现展开或折叠效果。

二、项目运行效果 

三、全部代码 

<!DOCTYPE HTML>
<HTML>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>折叠面板</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            text-decoration: none;
            list-style: none;
            background-repeat: no-repeat;
        }
        #collapse {
            width: 80%;
            margin: 10px auto;
            list-style-type: none;
            color: #333;
        }

        #collapse h2 {
            border: 1px solid #0c5087;
            background: #1069af;
            font-weight: normal;
            color: #ffffff;
            margin: 0;
            padding: 8px;
        }

        #collapse h3 {
            display: block;
            cursor: pointer;
            position: relative;
            margin: 2px 0 0;
            padding: .5em .5em .5em .7em;
            font-size: 100%;
            border-top-left-radius: 3px;
            border-top-right-radius: 3px;
            border: 1px solid #c5c5c5;
            background: #f6f6f6;
            font-weight: normal;
            color: #454545;
        }

        #collapse h3.active {
            border: 1px solid #0c5087;
            background: #1069af;
            font-weight: normal;
            color: #ffffff;
        }

        #collapse div {
            display: none;
            padding: 4px;
        }

    </style>
</head>

<body>
    <div id="collapse">
        <h2>问题知识列表</h2>
        <h3> 快递告知丢件,怎么处理?</h3>
        <div>
            <p>先货后款订单建议您重新下单购买;先款后货订单建议您提交退款申请,如您还需要该商品建议重新下单购买。</p>
        </div>

        <h3>配送过程中,物品损坏,怎么办?</h3>
        <div>
            <p>您好,分以下两种情况:</p>
            <p>1.签收前:您可以拒收商品;</p>
            <p> 2.签收后:如果您已经签收,建议您提交退换货申请,由专业的京东售后人员为您处理。</p>
        </div>

        <h3> 为何物流显示签收,却没有收到?</h3>
        <div>
            <p>若订单物流信息中显示已签收,实际却没有收到货,可能存在以下几种情况:</p>
            <p> 1、您的家人、朋友、公司前台、小区门卫等代收点帮忙代收,建议您优先查找下;</p>
            <p> 2、配送人员误操作已签收,建议您耐心等待配送人员为您送货;</p>
            <p>若您长时间未收货,京东自营商品建议您联系京东在线客服帮您查询原因,第三方卖家商品请直接联系商家客服处理。</p>
        </div>

        <h3>地址比较偏远,配送不到怎么办?</h3>
        <div>
            <p>非常抱歉,目前京东自营商品仅支持京东自营配送范围的送货服务,我们会不断扩大我们的配送范围,提升我们的服务。建议您后期持续关注。</p>
        </div>

        <h3>商品一直没收到,怎么办?</h3>
        <div>
            <p>您好,您可以使用“我要催单”催办对应的订单。操作路径如下:</p>
            <p>登陆电脑端京东商城首页—点击右上方“客户服务”—进入帮助中心首页—选择常用自助服务【我要催单】</p>
        </div>
        </li>
    </div>
</body>
    <script>
        const collapse = document.querySelector('#collapse');
        let h3s = document.querySelectorAll('h3');
        let divs = collapse.querySelectorAll('div');
        for (let i = 0; i < h3s.length; i++) {
            h3s[i].addEventListener('click', function () {
                let contDiv = this.nextElementSibling;
                if (contDiv.style.display == 'block') {
                    contDiv.style.display = 'none';
                    this.className = '';
                } else {
                    divs.forEach(function (cont) {
                        cont.style.display = 'none';
                        cont.previousElementSibling.className = '';
                    })
                    contDiv.style.display = 'block';
                    this.className = 'active';
                }
            })
        }
    </script>
</HTML>

四、答疑解惑

        这是一个非常适合练习JavaScript的小案例,各位小伙伴可以自行更改样式和内容,如果大家运行时出现问题或代码有什么不懂的地方都可以随时评论留言或联系博主。

        还多请各位小可爱多多点赞支持,你们的支持是我最大的动力。

博主VX:18884281851

谢谢各位的支持~~

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,以下是一个简单的前端HTML + CSS + JS注册和登录案例HTML代码: ``` <!DOCTYPE html> <html> <head> <title>注册和登录</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>欢迎注册和登录</h1> <div class="container"> <div class="form-container"> <h2>注册</h2> <form id="register-form" onsubmit="return validateRegisterForm()"> <label for="username">用户名:</label> <input type="text" name="username" id="username"> <label for="password">密码:</label> <input type="password" name="password" id="password"> <label for="email">电子邮件:</label> <input type="email" name="email" id="email"> <button type="submit">注册</button> </form> </div> <div class="form-container"> <h2>登录</h2> <form id="login-form" onsubmit="return validateLoginForm()"> <label for="username">用户名:</label> <input type="text" name="username" id="login-username"> <label for="password">密码:</label> <input type="password" name="password" id="login-password"> <button type="submit">登录</button> </form> </div> </div> <script src="script.js"></script> </body> </html> ``` CSS代码: ``` body { font-family: Arial, Helvetica, sans-serif; background-color: #f4f4f4; } h1 { text-align: center; margin-top: 50px; } .container { display: flex; flex-direction: row; justify-content: center; align-items: center; margin-top: 50px; } .form-container { background-color: #fff; padding: 20px; margin: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.3); } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="password"], input[type="email"] { width: 100%; padding: 10px; margin-bottom: 10px; border: none; border-radius: 5px; background-color: #f4f4f4; } button[type="submit"] { background-color: #4CAF50; color: #fff; padding: 10px; border: none; border-radius: 5px; cursor: pointer; } button[type="submit"]:hover { background-color: #3e8e41; } ``` JavaScript代码: ``` function validateRegisterForm() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; var email = document.getElementById("email").value; if (username == "") { alert("请输入用户名"); return false; } if (password == "") { alert("请输入密码"); return false; } if (email == "") { alert("请输入电子邮件"); return false; } alert("注册成功!"); return true; } function validateLoginForm() { var username = document.getElementById("login-username").value; var password = document.getElementById("login-password").value; if (username == "") { alert("请输入用户名"); return false; } if (password == "") { alert("请输入密码"); return false; } alert("登录成功!"); return true; } ``` 这个案例包含一个注册表单和一个登录表单。当用户提交表单时,JavaScript代码会验证输入是否有效,并根据结果显示成功或失败消息。您可以将此代码作为起点,进行更多的开发和改进,以实现更完整的注册和登录功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花花´◡`

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值