JavaScript简单的学生信息管理

界面样式

在这里插入图片描述

主要功能

  • 添加学生
  • 删除学生
  • 鼠标悬停时,将所在行变为绿色
  • 支持全选,反选,和全不选

代码部分

  • html
<html>

<head>
    <meta charset="UTF-8">
    <title>学生信息管理系统</title>
    <link rel="stylesheet" href="css/index.css">
</head>

<body>

    <div id="title">学生信息管理系统</div>
    <div id="input-box">
        <input id="id" type="text" placeholder="请输入学号" name="id">
        <input id="name" type="text" placeholder="请输入姓名" name="name">
        <input id="gender" type="text" placeholder="请输入性别" name="gender">
        <button id="add">添加</button>
    </div>

    <br>
    <br>

    <div id="table-box">
        <div id="title1">学生信息表
            <br>
            <button id="arcall">反选</button>
            <button id="empty">全不选</button>
            <button id="all">全选</button>
        </div>
        <table id="stutable">
            <tr id="row1">
                <td></td>
                <td>编号</td>
                <td>姓名</td>
                <td>性别</td>
                <td>操作</td>
            </tr>
        </table>
    </div>

</body>

<footer>
    <script src="js/index.js"></script>
</footer>

</html>
  • css
div {
    width: 100%;
    text-align: center;
    margin: auto;
}

#title {
    font-weight: 700;
}

#stutable {
    width: 50%;
    margin: 3px auto;
    border: 1px solid black;
    text-align: center;
}

#stutable tr td {
    width: 120px;
    text-align: center;
    border: 1px solid black;
}

#title1 {
    margin: auto auto;
    font-weight: 700;
    width: 30%;
}

#title1 button {
    
}
  • javascript
var studentAddButton = document.getElementById("add");

function createCell(name) {
    var message = document.getElementById(name);
    var textNode = document.createTextNode(message.value);
    var cell = document.createElement("td");
    cell.appendChild(textNode);
    return cell;
}


function createDeleteCell() {
    var textNode = document.createTextNode("删除");
    var deleteCell = document.createElement("a");
    deleteCell.appendChild(textNode);
    deleteCell.setAttribute("href", "javascript:void(0);");
    deleteCell.setAttribute("id", "delete");

    deleteCell.onclick = function () {
        var parent = this.parentNode;
        var tmp = parent = parent.parentNode;
        parent = parent.parentNode;
        parent.removeChild(tmp);
    }

    var cell = document.createElement("td");
    cell.appendChild(deleteCell);

    return cell
}

function createSmallCell() {
    var box = document.createElement("input");
    box.setAttribute("type", "checkbox");
    box.setAttribute("class", "cbox");

    var smallCell = document.createElement("td");
    smallCell.appendChild(box);
    return smallCell;
}

studentAddButton.onclick = function () {
    var table = document.getElementById("stutable");
    var nextRow = document.createElement("tr");

    nextRow.appendChild(createSmallCell());

    nextRow.appendChild(createCell("id"));
    nextRow.appendChild(createCell("name"));
    nextRow.appendChild(createCell("gender"));
    nextRow.appendChild(createDeleteCell());

    nextRow.onmouseover = function () {
        nextRow.style.backgroundColor = "yellowgreen";
    }

    nextRow.onmouseout = function () {
        nextRow.style.backgroundColor = "white"
    }

    table.appendChild(nextRow);
}

var all = document.getElementById("all");
all.onclick = function () {
    var checkboxList = document.getElementsByClassName("cbox");
    for (var i = 0; i < checkboxList.length; i++) {
        var box = checkboxList[i];
        if (!box.checked) {
            box.click();
        }
    }
}

document.getElementById("empty").onclick = function () {
    var checkboxList = document.getElementsByClassName("cbox");
    for (var i = 0; i < checkboxList.length; i++) {
        var box = checkboxList[i];
        if (box.checked) {
            box.click();
        }
    }
}

document.getElementById("arcall").onclick = function () {
    var checkboxList = document.getElementsByClassName("cbox");
    for (var i = 0; i < checkboxList.length; i++) {
        checkboxList[i].click();
    }
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值