小工具知识网

当前位置: 首页 > 行业知识 > 前端小案例——购买电影票(HTML+CSS+JS, 附源码)

行业知识

前端小案例——购买电影票(HTML+CSS+JS, 附源码)

2026-11-05 15:11:43 来源:互联网转载

一、前言

实现功能:

这段代码实现了一个简单的电影票选座购买的功能界面。

  1. 在页面上展示了一个电影院的座位布局,以及右侧显示了电影信息、选座情况、票价、总计等内容。

  2. 用户可以通过点击座位来选择购买电影票,每个座位的状态会在点击时改变(从默认颜色变为红色),并在右侧显示已选座位的信息。

  3. 用户可以确认购买,确认购买后会出现购买成功的提示,并且页面会重置,清空已选座位和右侧信息。

实现逻辑:

  1. 首先,在 HTML 中定义了一个包含座位和右侧信息的页面结构。

  2. 使用 JavaScript 获取到需要操作的元素,包括座位列表、座位元素、默认座位颜色、选座数目、选座信息展示区域等。

  3. 使用循环创建了 64 个座位,并给每个座位添加了行列信息。同时初始化了默认座位颜色和选座数目变量。

  4. 定义了一个函数 addSeatClick,用于处理座位的点击事件。当座位被点击时,根据当前座位状态(默认或已选),改变座位颜色、更新选座数目、显示已选座位信息以及计算总金额等操作。

  5. 遍历所有座位元素,为每个座位添加点击事件处理函数。

  6. 监听确认购买按钮的点击事件。点击确认购买后,弹出购买成功提示,重置所有座位颜色,清空已选座位信息和总金额,并将选座数目重置为零。

二、项目运行效果

三、全部代码

            购买电影票            * {            margin: 0;            padding: 0;            text-decoration: none;            list-style: none;            background-repeat: no-repeat;        }        .con-box {            width: 100%;            height: 100vh;            display: flex;            justify-content: center;            gap: 10px;            margin: 0 auto;        }        .left {            width: 60%;            height: 100vh;            border-right: 1px dashed #000;        }        .left p {            width: 70%;            height: 35px;            line-height: 35px;            text-align: center;            margin: 30px auto;            background-color: #f0f0f0;            border-radius: 5px;        }        .seat {            display: flex;            width: 80%;            height: 750px;            flex-wrap: wrap;            gap: 30px;            justify-content: space-around;            align-content: center;            margin: 0 auto;        }        .seat li {            width: 8%;            height: 8%;            border-radius: 5px;            background-color: #b9ef9f;            line-height: 60px;            text-align: center;            cursor: pointer;        }        .right {            width: 35%;            height: 100vh;            color: #a79e9f;            position: relative;        }        .right-con {            width: 350px;            height: 90vh;            position: absolute;            right: 0;            top: 5%;            line-height: 28px;        }        #seatNumbers {            width: 240px;        }        .seat-number {            display: inline-block;            width: 100px;            height: 30px;            background-color: #efefef;            border-radius: 5px;            text-align: center;            margin-left: 10px;            margin-bottom: 10px;            line-height: 30px;            color: #000;            border: #d1d1d1 1px solid;        }        .right-con button {            width: 70px;            height: 25px;            margin: 25px 50px;            background-color: #efefef;            border: solid 0.5px #000;            border-radius: 2px;            cursor: pointer;        }                            

屏幕

    • 影片:后来的我们
    • 时间:5月1日 21:00
    • 票价:50元
    • 座位:
    • 票数:
    • 总计:
    • 确认购买
    document.addEventListener('DOMContentLoaded', function () { const seatList = document.querySelector('.seat'); for (let i = 0; i { alert('购买成功!'); seats.forEach(seat => { seat.style.backgroundColor = '#b9ef9f'; }); seatNumbers.innerHTML = ''; selectedSeats = []; document.querySelector('.right-con li:nth-child(5) span').innerText = ''; document.querySelector('.right-con li:nth-child(6) span').innerText = ''; }); } but() seats.forEach(seat => { addSeatClick(seat); }); });

    四、答疑解惑

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

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

    博主VX:18884281851

    谢谢各位的支持~~

                            

     

    上一篇:Jmeter连接不同类型数据库语法

    下一篇:桃子复数怎么写英语?