利用html加css以及JavaScript写一个学生后台管理系统简单平台

之前学习的时候做了一个简单的crud,下面是效果图
在这里插入图片描述
html内容如下

<div class="conter">
    <div>
        <button id="btnAdd">添加</button>
        <button id="delAll" onclick="delAll()"> 全部删除</button>
    </div>
    <table border="1" cellpadding="0" cellspacing="0">
        <thead>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>班级</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tbStudent">

        </tbody>

    </table>
    <div id="divAddStudent" class="hidden">
        <h2>学生信息</h2>
        <form action="#" id="addStudentForm">
            <p>学号:<input type="text" name="stuNo" id="stuNo"> </p>
            <p>姓名:<input type="text" name="tuName" id="stuName"> </p>
            <p>年龄:<input type="text" name="stuAge" id="stuAge"> </p>
            <p>班级:<input type="text" name="stuClass" id="stuClass"> </p>
            <p>    <button id="btnAddData" type="button">添加</button>  <button  id="btnCancel">取消</button></p>
        </form>
    </div>

</div>
<div id="zhezhao"  class="hidden"></div>

css内容

		*{
            margin: 0;
            padding: 0;
        }
        table{
            width: 100%;
        }
        table th ,table td{
            height: 30px;
            line-height: 30px;
        }
        table tbody tr:nth-child(2n){
            background-color: gainsboro;
        }
        table td{
            text-align: center;
        }
        .conter{
            width: 960px;
            margin: 30px auto;
        }
        button{
            padding: 5px 10px;
            margin: 5px;
            background-color: #00A398;
            color: white;
            border: none;
            border-radius: 15%;
            cursor: pointer;
        }
        #divAddStudent{
            position: absolute;
            z-index: 1;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
            width: 500px;
            height: 300px;
            margin: auto;
            background-color: white;
            box-sizing: border-box;
            padding: 20px 50px;
        }
        #divAddStudent h2{
            margin-bottom: 30px;
        }
        #divAddStudent p{
            margin-top: 10px;
        }
        #zhezhao{
            position: absolute;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.4);
        }
        .hidden{
            display: none;
        }
        .show{
            display: block;
        }
        tbody:empty:before{
            content:"当前没有学生,快去添加吧"
        }

js内容

// 学生数据 stuNo:编号(唯一)stuName:姓名,stuAge:年龄,stuClass:班级名称
var studentArr=[  
       {"stuNo":"001","stuName":"红","stuAge":16,"stuClass":"218"},
        {"stuNo":"002","stuName":"小米","stuAge":15,"stuClass":"219"},
        {"stuNo":"003","stuName":"小明","stuAge":15,"stuClass":"218"},
        {"stuNo":"004","stuName":"小蓝","stuAge":16,"stuClass":"218"},
        {"stuNo":"005","stuName":"小紫","stuAge":15,"stuClass":"219"},
        {"stuNo":"006","stuName":"小青","stuAge":15,"stuClass":"218"},
        {"stuNo":"007","stuName":"小白","stuAge":16,"stuClass":"218"},
        {"stuNo":"008","stuName":"小黄","stuAge":15,"stuClass":"219"},
        {"stuNo":"009","stuName":"小黑","stuAge":15,"stuClass":"218"}
    ];
    var stuNoOper="";
    dataStudent();
    $("btnAdd").onclick=AddStudent;
    $("zhezhao").onclick=function () {
        model("divAddStudent","hidden");
    };
    $("btnCancel").onclick=function () {
        model("divAddStudent","hidden");
    };
    $("btnAddData").onclick=addStudentData;
    //添加学生
    function addStudentData() {
        var stuNo =$("stuNo").value;
        var stuName =$("stuName").value;
        var stuAge =$("stuAge").value;
        var stuClass =$("stuClass").value;
        if(stuNo==""){
            alert("学号不能为空");
            return;
        }else if (stuName==""){
            alert("姓名不能为空");
            return;
        }else if (stuAge==""){
            alert("年龄不能为空");
            return;
        }else if (stuClass==""){
            alert("班级不能为空");
            return;
        }
        if (stuNoOper==""){
            if (checkStudentNo(stuNo)){
                alert("当前学号已经存在");
                return;
            }
            var  obj= {"stuNo":stuNo,"stuName":stuName,"stuAge":stuAge,"stuClass":stuClass};
            studentArr.push(obj);
            if (confirm("添加成功,是否继续添加")){
                $("stuNo").value="";
                $("stuName").value="";
                $("stuAge").value="";
                $("stuClass").value="";
                dataStudent();
            }  else {
                model("divAddStudent","hidden");
                dataStudent();
            }
        }
        else {
            for(var i=0;i<studentArr.length;i++){
                if (studentArr[i].stuNo==stuNoOper){
                    studentArr[i].stuName=stuName;
                    studentArr[i].stuAge=stuAge;
                    studentArr[i].stuClass=stuClass;
                    alert("修改成功");
                    model("divAddStudent","hidden");
                    dataStudent();
                    break;
                }
            }
        }


    }
    //模态框方法
    function model(id,type) {
        type=type || "show";
        $("zhezhao").className=type;
        $(id).className=type;
    }
    //修改学生
    function  update(stuno) {
        console.log(123)
        var stuObj=getStudentBystuNO(stuno);
        if(stuObj){
            stuNoOper=stuno;
            model("divAddStudent");
            $("stuNo").value=stuObj.stuNo;
            $("stuName").value=stuObj.stuName;
            $("stuAge").value=stuObj.stuAge;
            $("stuClass").value=stuObj.stuClass;
        }else {
            alert("输入有误")
        }
    }
    //获取修改学生信息
    function getStudentBystuNO(stuno) {
        for(var i=0;i<studentArr.length;i++){
            if(studentArr[i].stuNo==stuno){
                return studentArr[i];
            }
        }
        return null;
    }
    //添加,接收用户数据
    function AddStudent() {
        stuNoOper="";
        model("divAddStudent");
    }
    //数据显示
    function dataStudent() {
        var tbstudent=$("tbStudent");
        tbstudent.innerHTML="";
        for(var i=0; i<studentArr.length;i++){
                tbstudent.innerHTML+=`<tr><td>${studentArr[i].stuNo}</td><td>${studentArr[i].stuName}</td><td>${studentArr[i].stuAge}</td><td>${studentArr[i].stuClass}</td> 
                <td><button onclick = 'del(${studentArr[i].stuNo})'>删除</button><button onclick = 'update(${studentArr[i].stuNo})'>修改</button></td></tr>`
        }
    }
    //删除
    function del(stuno) {
        if (confirm("确定要删除吗")){
            for (var i=0;i<studentArr.length;i++){
                if(studentArr[i].stuNo==stuno){
                    studentArr.splice(i,1);
                    alert("删除成功");
                    dataStudent();
                    break;
                }
            }
        }
    }
    //删除全部
    function delAll() {
        if (confirm("确定要删除吗")){
            studentArr=[];
            dataStudent();
        }
    }
    //验证学号
    function checkStudentNo(stuNo) {
        for( var i=0;i<studentArr.length;i++){
            if(studentArr[i].stuNo==stuNo){
                return true;
            }
        }
        return false;
    }
    //选择id
    function $(id) {
        return document.getElementById(id);
    }

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        table{
            width: 100%;
        }
        table th ,table td{
            height: 30px;
            line-height: 30px;
        }
        table tbody tr:nth-child(2n){
            background-color: gainsboro;
        }
        table td{
            text-align: center;
        }
        .conter{
            width: 960px;
            margin: 30px auto;
        }
        button{
            padding: 5px 10px;
            margin: 5px;
            background-color: #00A398;
            color: white;
            border: none;
            border-radius: 15%;
            cursor: pointer;
        }
        #divAddStudent{
            position: absolute;
            z-index: 1;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
            width: 500px;
            height: 300px;
            margin: auto;
            background-color: white;
            box-sizing: border-box;
            padding: 20px 50px;
        }
        #divAddStudent h2{
            margin-bottom: 30px;
        }
        #divAddStudent p{
            margin-top: 10px;
        }
        #zhezhao{
            position: absolute;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.4);
        }
        .hidden{
            display: none;
        }
        .show{
            display: block;
        }
        tbody:empty:before{
            content:"当前没有学生,快去添加吧"
        }

    </style>
</head>
<body>
<div class="conter">
    <div>
        <button id="btnAdd">添加</button>
        <button id="delAll" onclick="delAll()"> 全部删除</button>
    </div>
    <table border="1" cellpadding="0" cellspacing="0">
        <thead>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>班级</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tbStudent">

        </tbody>

    </table>
    <div id="divAddStudent" class="hidden">
        <h2>学生信息</h2>
        <form action="#" id="addStudentForm">
            <p>学号:<input type="text" name="stuNo" id="stuNo"> </p>
            <p>姓名:<input type="text" name="tuName" id="stuName"> </p>
            <p>年龄:<input type="text" name="stuAge" id="stuAge"> </p>
            <p>班级:<input type="text" name="stuClass" id="stuClass"> </p>
            <p>    <button id="btnAddData" type="button">添加</button>  <button  id="btnCancel">取消</button></p>
        </form>
    </div>

</div>
<div id="zhezhao"  class="hidden"></div>
<script type="text/javascript">
    var studentArr=[  //数组
        {"stuNo":"001","stuName":"小红","stuAge":16,"stuClass":"218"},
        {"stuNo":"002","stuName":"小米","stuAge":15,"stuClass":"219"},
        {"stuNo":"003","stuName":"小明","stuAge":15,"stuClass":"218"},
        {"stuNo":"004","stuName":"小蓝","stuAge":16,"stuClass":"218"},
        {"stuNo":"005","stuName":"小紫","stuAge":15,"stuClass":"219"},
        {"stuNo":"006","stuName":"小青","stuAge":15,"stuClass":"218"},
        {"stuNo":"007","stuName":"小白","stuAge":16,"stuClass":"218"},
        {"stuNo":"008","stuName":"小黄","stuAge":15,"stuClass":"219"},
        {"stuNo":"009","stuName":"小黑","stuAge":15,"stuClass":"218"}
    ];
    var stuNoOper="";
    dataStudent();
    $("btnAdd").onclick=AddStudent;
    $("zhezhao").onclick=function () {
        model("divAddStudent","hidden");
    };
    $("btnCancel").onclick=function () {
        model("divAddStudent","hidden");
    };
    $("btnAddData").onclick=addStudentData;
    //添加学生
    function addStudentData() {
        var stuNo =$("stuNo").value;
        var stuName =$("stuName").value;
        var stuAge =$("stuAge").value;
        var stuClass =$("stuClass").value;
        if(stuNo==""){
            alert("学号不能为空");
            return;
        }else if (stuName==""){
            alert("姓名不能为空");
            return;
        }else if (stuAge==""){
            alert("年龄不能为空");
            return;
        }else if (stuClass==""){
            alert("班级不能为空");
            return;
        }
        if (stuNoOper==""){
            if (checkStudentNo(stuNo)){
                alert("当前学号已经存在");
                return;
            }
            var  obj= {"stuNo":stuNo,"stuName":stuName,"stuAge":stuAge,"stuClass":stuClass};
            studentArr.push(obj);
            if (confirm("添加成功,是否继续添加")){
                $("stuNo").value="";
                $("stuName").value="";
                $("stuAge").value="";
                $("stuClass").value="";
                dataStudent();
            }  else {
                model("divAddStudent","hidden");
                dataStudent();
            }
        }
        else {
            for(var i=0;i<studentArr.length;i++){
                if (studentArr[i].stuNo==stuNoOper){
                    studentArr[i].stuName=stuName;
                    studentArr[i].stuAge=stuAge;
                    studentArr[i].stuClass=stuClass;
                    alert("修改成功");
                    model("divAddStudent","hidden");
                    dataStudent();
                    break;
                }
            }
        }


    }
    //模态框方法
    function model(id,type) {
        type=type || "show";
        $("zhezhao").className=type;
        $(id).className=type;
    }
    //修改学生
    function  update(stuno) {
        console.log(123)
        var stuObj=getStudentBystuNO(stuno);
        if(stuObj){
            stuNoOper=stuno;
            model("divAddStudent");
            $("stuNo").value=stuObj.stuNo;
            $("stuName").value=stuObj.stuName;
            $("stuAge").value=stuObj.stuAge;
            $("stuClass").value=stuObj.stuClass;
        }else {
            alert("输入有误")
        }
    }
    //获取修改学生信息
    function getStudentBystuNO(stuno) {
        for(var i=0;i<studentArr.length;i++){
            if(studentArr[i].stuNo==stuno){
                return studentArr[i];
            }
        }
        return null;
    }
    //添加,接收用户数据
    function AddStudent() {
        stuNoOper="";
        model("divAddStudent");
    }
    //数据显示
    function dataStudent() {
        var tbstudent=$("tbStudent");
        tbstudent.innerHTML="";
        for(var i=0; i<studentArr.length;i++){
                tbstudent.innerHTML+=`<tr><td>${studentArr[i].stuNo}</td><td>${studentArr[i].stuName}</td><td>${studentArr[i].stuAge}</td><td>${studentArr[i].stuClass}</td> 
                <td><button onclick = 'del(${studentArr[i].stuNo})'>删除</button><button onclick = 'update(${studentArr[i].stuNo})'>修改</button></td></tr>`
        }
    }
    //删除
    function del(stuno) {
        if (confirm("确定要删除吗")){
            for (var i=0;i<studentArr.length;i++){
                if(studentArr[i].stuNo==stuno){
                    studentArr.splice(i,1);
                    alert("删除成功");
                    dataStudent();
                    break;
                }
            }
        }
    }
    //删除全部
    function delAll() {
        if (confirm("确定要删除吗")){
            studentArr=[];
            dataStudent();
        }
    }
    //验证学号
    function checkStudentNo(stuNo) {
        for( var i=0;i<studentArr.length;i++){
            if(studentArr[i].stuNo==stuNo){
                return true;
            }
        }
        return false;
    }
    //选择id
    function $(id) {
        return document.getElementById(id);
    }



</script>
</body>
</html>
  • 12
    点赞
  • 149
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个完整的后台管理系统需要包括用户登录、数据展示、数据编辑、权限控制等多个模块。以下是一个简单的示例,包含了用户登录和数据展示两个模块,使用了 HTMLCSSJavaScript: 1. 登录页面 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>后台管理系统-登录</title> <style> body { background-color: #f5f5f5; font-family: Arial, sans-serif; } .login-form { margin: 100px auto; width: 300px; padding: 20px; background-color: #fff; border: 1px solid #ccc; } .login-form h2 { text-align: center; margin-bottom: 20px; } .login-form label { display: block; margin-bottom: 10px; } .login-form input[type="text"], .login-form input[type="password"] { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; margin-bottom: 20px; } .login-form input[type="submit"] { background-color: #4CAF50; color: #fff; border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; } .login-form input[type="submit"]:hover { background-color: #3e8e41; } </style> </head> <body> <div class="login-form"> <h2>后台管理系统</h2> <form> <label>用户名:</label> <input type="text" name="username" required> <label>密码:</label> <input type="password" name="password" required> <input type="submit" value="登录"> </form> </div> </body> </html> ``` 2. 数据展示页面 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>后台管理系统-数据展示</title> <style> body { background-color: #f5f5f5; font-family: Arial, sans-serif; } .header { background-color: #4CAF50; color: #fff; padding: 20px; text-align: center; } .container { margin: 20px auto; width: 80%; background-color: #fff; border: 1px solid #ccc; padding: 20px; } .table { border-collapse: collapse; width: 100%; } .table th, .table td { border: 1px solid #ccc; padding: 10px; } .table th { background-color: #f5f5f5; font-weight: bold; } </style> </head> <body> <div class="header"> <h1>后台管理系统</h1> <p>欢迎您,admin</p> </div> <div class="container"> <h2>用户列表</h2> <table class="table"> <thead> <tr> <th>ID</th> <th>用户名</th> <th>邮箱</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>user1</td> <td>user1@example.com</td> <td> <a href="#">编辑</a> <a href="#">删除</a> </td> </tr> <tr> <td>2</td> <td>user2</td> <td>user2@example.com</td> <td> <a href="#">编辑</a> <a href="#">删除</a> </td> </tr> <tr> <td>3</td> <td>user3</td> <td>user3@example.com</td> <td> <a href="#">编辑</a> <a href="#">删除</a> </td> </tr> </tbody> </table> </div> </body> </html> ``` 3. JavaScript 代码 ```javascript // 登录表单提交事件 document.querySelector('form').addEventListener('submit', function(e) { e.preventDefault(); // 阻止表单提交 var username = this.username.value; var password = this.password.value; // 向服务器发送登录请求 // 这里只是模拟,实际应用中需要向后端发送请求并验证用户名和密码 if (username === 'admin' && password === 'admin') { location.href = 'data.html'; // 登录成功,跳转到数据展示页面 } else { alert('用户名或密码错误!'); // 登录失败,显示错误提示 } }); ``` 这个示例包含了一个简单的登录页面和一个数据展示页面。当用户在登录页面输入正确的用户名和密码后,会跳转到数据展示页面。在实际应用中,需要上权限控制、数据编辑等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值