用JavaScript实现对表格的增删改与上移、下移、增加、减少功能

用JavaScript实现对表格的增删改与上移、下移、增加、减少功能
学了几个星期的JS实现了对表格的增删改以及其他操作,来检验自己的学习成果。

效果图:
在这里插入图片描述

下面代码含有jquery,所以要导入jquery的模块,并且改一下:
附上源码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生成绩</title>
</head>
<style>
    * {
        margin: 2px;
    }

    body {
        background-color: rgb(227, 227, 219);
    }

    .main {
        width: 500px;
        height: 100%;
        background-color: rgb(183, 218, 246);
        margin-left: 380px;
    }

    .div1 {
        padding-top: 50px;
        margin-left: 111px;
    }

    .div2 {
        margin-top: 20px;
        margin-left: 75px;
        padding-bottom: 70px;
    }

    table {
        border: 1px gray solid;
        border-spacing: 0px;
        border-collapse: collapse;
        text-align: center;
    }

    table .tr1 {
        background-color: antiquewhite;
    }

    table td {
        border: 1px gray solid;
        padding: 5px;
    }

    img {
        width: 25%;
        height: 25%;
        display: inline;
    }

    .b1 {
        font-size: 25px;
        margin-left: 225px;
    }

    .table-operation {
        width: 150px;
        height: 70px;
    }
</style>

<body>
   <!--  <img src="logo.png" alt=""> -->
    <b class="b1">学生成绩表</b>
    <div class="main">
        <div class="div1">
            <b>姓名:</b>
            <input type="text" id="ipt1" class="ipt"><br>
            <b>科目:</b>
            <select name="type" type="text" id="ipt2" class="ipt">
                <option velue="javascript">javascript</option>
                <option value="HTML">HTML</option>
                <option value="计算机组成原理">计算机组成原理</option>
                <option value="高等数学">高等数学</option>
                <option value="大学英语">大学英语</option>
                <option value="算法与数据结构">算法与数据结构</option>
            </select><br>
            <b>成绩:</b>
            <input type="text" id="ipt3" class="ipt"><br>
            <button id="btn" style="margin-left:175px;">提交</button>
        </div>
        <div class="div2">
            <table id="tb">
                <tbody id="to">
                    <tr class="tr1" id="row0">
                        <td>姓名</td>
                        <td>科目</td>
                        <td>成绩</td>
                        <td>操作</td>
                    </tr>
                    <tr class="tr1">
                        <td>张三</td>
                        <td>javascript</td>
                        <td>90</td>
                        <td class='table-operation'>
                            <input type='button' value='删除' class='remove'>
                            <input type='button' value='修改' class="modify">
                            <input type='button' value='上移' class='up'>
                            <input type="button" value="增加" class="z">
                            <input type="button" value="减少" class="j">
                            <input type='button' value='下移' class='down'>
                        </td>
                    </tr>
                    <tr class="tr1">
                        <td>李四</td>
                        <td>javascript</td>
                        <td>99</td>
                        <td class='table-operation'>
                            <input type='button' value='删除' class='remove'>
                            <input type='button' value='修改' class="modify">
                            <input type='button' value='上移' class='up'>
                            <input type="button" value="增加" class="z">
                            <input type="button" value="减少" class="j">
                            <input type='button' value='下移' class='down'>
                        </td>
                    </tr>
                    <tr class="tr1">
                        <td>王五</td>
                        <td>javascript</td>
                        <td>95</td>
                        <td class='table-operation'>
                            <input type='button' value='删除' class='remove'>
                            <input type='button' value='修改' class="modify">
                            <input type='button' value='上移' class='up'>
                            <input type="button" value="增加" class="z">
                            <input type="button" value="减少" class="j">
                            <input type='button' value='下移' class='down'>
                        </td>
                    </tr>
                    </tobody>
            </table>
        </div>
    </div>
    <script src="jquery.js"></script>
    <script>

        // 增加+1
        $(document).on('click', '.z', function () {
            var o = document.getElementsByClassName("tr1");
            var a = $(".z").index(this) + 1;
            o[a].children[2].innerHTML = parseInt(o[a].children[2].innerHTML) + 1;
        });

        //减少-1
        $(document).on('click', '.j', function () {
            var o = document.getElementsByClassName("tr1");
            var a = $(".j").index(this) + 1;
            o[a].children[2].innerHTML = parseInt(o[a].children[2].innerHTML) - 1;
        });

        // 上移动
        $(document).on('click', '.up', function () {
            var ind = $(this).parents('tr').index();
            console.log(ind);
            if (ind == 1) {
                alert('已经是最上层了!');
                return false;
            }
            var trs = $(this).parents('tr').clone(true);
            console.log(trs);

            $(this).parents('table tbody').find('tr').eq(ind - 1).before(trs);
            $(this).parents('tr').remove();
            //重新添加序号
            drawOrder()
        });
        //下移动
        $(document).on('click', '.down', function () {
            var ind = $(this).parents('tr').index();
            if (($(this).parents("tr").index()) == ($(this).parents("table tbody").find("tr").length - 1)) {
                alert('已经是最下层了!');
                return false;
            }
            var trs = $(this).parents('tr').clone(true);
            $(this).parents('table tbody').find('tr').eq(ind + 1).after(trs);
            $(this).parents('tr').remove();
            //重新添加序号
            drawOrder();
        });
        //==================================================================== 
        var ipt = document.getElementsByClassName("ipt");
        var btn = document.getElementById("btn");
        var tb = document.getElementById("tb");
        var btn2 = document.getElementsByClassName("btn-default");
        var ipt1 = document.getElementById('ipt1')
        var ipt3 = document.getElementById('ipt3')


        function fun() {
            // 判断姓名、成绩是否合法
            if (ipt1.value.length > 0 && ipt1.value.length < 6 && Number.parseFloat(ipt3.value) >= 0 && Number.parseFloat(ipt3.value) <= 100) {
                // console.log(typeof ipt1.value);
                // console.log(ipt1.value.length);
                console.log(Number.parseFloat(ipt3.value));
                var newTr = tb.insertRow();
                var newTd0 = newTr.insertCell();
                var newTd1 = newTr.insertCell();
                var newTd2 = newTr.insertCell();
                var newTd3 = newTr.insertCell();

                var to = document.getElementById('to')
                newTr.className = "tr1"
                console.log(newTr);
                newTd0.innerText = ipt[0].value;
                newTd1.innerText = ipt[1].value;
                newTd2.innerText = ipt[2].value;
                newTd3.innerHTML = "<input type='button' value= '删除'  class = 'remove'> <input type='button' value='修改' class='modify'>    <input type='button' value='上移' class='up'>  <input type='button' value='增加' id='zj' class='z'> <input type='button' value='减少' class='j'>  <input type='button' value='下移' class='down'> ";
                console.log(newTd2);

                // 点击提交后 消除除了科目表单的input
                for (let j = 0; j < ipt.length; j++) {
                    if (j == 1) {
                        continue;
                    } else {
                        ipt[j].value = "";
                    }
                }
                ipt[0].focus();
            } else {
                if (ipt1.value.length < 1) {
                    alert('姓名不能为空!')
                }
                if (ipt1.value.length > 5) {
                    alert('姓名太长了!');
                }
                if (Number.parseFloat(ipt3.value) < 0 || Number.parseFloat(ipt3.value) > 100) {
                    alert('成绩只能在1~100以内!')
                }
            }
            // console.log("姓名 " + ipt1.value.length + typeof ipt.value);

        }
        btn.onmouseup = fun;
        //获取光标  
        ipt[0].focus();
        //回车换行
        ipt[0].onkeyup = function (ev) {
            if (ev.key == "Enter") {
                ipt[2].focus();
            }
        }
        ipt[2].onkeyup = function (ev) {
            if (ev.key == "Enter") {
                fun();
            }
        }
        //删除操作  jquery
        $(document).on('click', '.remove', function () {
            if (window.confirm("您确定要删除数据吗?")) {
                $(this).parent().parent().remove();
            }
        })

        //修改操作 
        $(document).on('click', '.modify', function () {
            // console.log($(this).val());
            if ($(this).val() == '修改') {
                $(this).parent().siblings("td").each(function () {
                    var is_text = $(this).find("input:text");//判断单元格下是否含有文本框
                    if (!is_text.length) {
                        $(this).html("<input size = '2' type='text'  class='t' value=' " + $(this).text() + " ' />");
                    }
                    else { $(this).html(is_text.val()); }
                })
                $(this).val("保存");
            } else {
                $(this).parent().siblings("td").each(function () {
                    var is_text = $(this).find("input:text");//判断单元格下是否含有文本框
                    if (!is_text.length) {
                        $(this).html("<input size = '2' type='text'  class='t' value=' " + $(this).text() + " ' />");
                    }
                    else { $(this).html(is_text.val()); }
                })
                $(this).val("修改");
            }
        });
    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值