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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章