jQuery 页面新增 与 删除

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-3.3.1.js"></script>
    <style>
        tr, th, table, td {
            border: 1px solid pink;

        }

        a {
            display: inline-block;
            font-size: 14px;
            width: 40px;
            margin: 2px;
            text-align: center;
            height: 22px;
            line-height: 22px;
            background-color: #EFEFEF;
        }

        .first {
            background-color: #ffa275;
            width: 40%;
            height: auto;
            position: absolute;
            left: 35%;
        }

        table {
            text-align: center;
            width: auto;
            height: auto;
        }

        .bgc {

            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: gray;
            opacity: 0.5;
            display: none;
        }

        .newData {
            position: absolute;
            width: 50%;
            height: auto;
            background-color: #9F6;
            margin: 0 auto;
            left: 25%;
            top: 30%;
            display: none;
        }


        .title {
            width: 50%;
            height: 30px;
            margin: 0 auto;
            background-color: #0F6;
        }

        .middle {
            text-align: center;
            width: 50%;
            height: auto;
            margin: 0 auto;
            background-color: pink;
        }

        .bottom {
            width: 50%;

            text-align: center;
            height: auto;
            background-color: blue;
            margin: 0 auto;
        }

        #span1 {

            float: left;
         

            padding-left: 15px;
        }

        #span2 {

            float: right;
            /* 鼠标放的样式*/
            cursor: pointer;
            padding-right: 5px;
        }

    </style>
    <script>
        $(function () {
            // dom节点加载完自动执行
            $("tr:odd").css("background-color", "yellow");
            // 设置方法       a:nth-child(2)--第二个a标签
            $("tbody").on("click", "tr td a:nth-child(2)", function () {
                if (confirm("确定删除?")) {
                    $("tr:odd").css("background-color", " #ffa275");
                    $(this).parents("tr").remove();
                    $("tr:odd").css("background-color", "yellow");
                }
                if ($("tbody").tr.length === 1) {
                    $("tbody").remove();
                }
            });

// 全选框     thead tr th  input:checkbox  获取thead 标签里面 tr 里面的 th 里面的  input里面的 复选框
            $("thead tr th  input:checkbox").click(function () {
                // 判断是否被选中       $(this).prop("checked") 返回类型是true或 false
                if ($(this).prop("checked")) {
                    if (confirm("确定全选?"))
                        $("tbody input:checkbox").prop("checked", true);
                } else {
                    $("tbody input:checkbox").prop("checked", false);
                }
            });

            //点击新增 页面出来
            $("#button1").click(function () {
                $(".bgc").show();
                $(".newData").show();
            });

            // 点击X  页面隐藏
            $("#span2").click(function () {
                $(".bgc").hide();
                $(".newData").hide();
            });

            $("#button2").click(function () {
                // 获取参数
                var name = $("#name").val();
                var subject = $("#subject").val();
                var goal = $("#goal").val();
                console.log(name);
                if(name==""){
                    alert("名字不能为空");
                    return  ;
                }
                $("tbody").append("<tr>" +
                    "<td><input type='checkbox'></td>" +
                    "<td>" + name + "</td>" +
                    "<td>" + subject + "</td>" +
                    "<td>" + goal + "</td>" +
                    "<td><a>修改</a><a>删除</a></td>" +
                    "</tr>");


                $(".bgc").hide();
                $(".newData").hide();
                $("tr:odd").css("background-color", "yellow");
            });

        });

    </script>
</head>
<body>

<div class="first">
    <button id="button1">新增</button>
    <div class="content">
        <table>
            <thead>
            <tr>
                <th><input type="checkbox"></th>
                <th>名字</th>
                <th>科目</th>
                <th>分数</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td><input type="checkbox"></td>
                <td>李小华</td>
                <td>数学</td>
                <td>100</td>
                <td><a>修改</a><a>删除</a></td>
            </tr>

            <tr>
                <td><input type="checkbox"></td>
                <td>李中华</td>
                <td>数学</td>
                <td>72</td>
                <td><a>修改</a><a>删除</a></td>
            </tr>

            <tr>
                <td><input type="checkbox"></td>
                <td>李大华</td>
                <td>数学</td>
                <td>60</td>
                <td><a>修改</a><a>删除</a></td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
<div class="bgc">

</div>
<div class="newData">
    <div class="title">
        <span id="span1">添加标题</span>
        <span id="span2">X</span>
    </div>


    <div class="middle">
        请填写名字:<input type="text" id="name">
        <br><br>
        请填写科目:<input type="text" id="subject">
        <br><br>
        请填写分数:<input type="text" id="goal">
        <br><br>
    </div>


    <div class="bottom">
        <button id="button2">提交</button>
    </div>

</div>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值