• 简单实现一个todoList(上移、下移、置顶、置底)


    演示

    html部分请添加图片描述

    <!DOCTYPE html>
    <html>
    <head>
        <title>表格示例</title>
    </head>
    <body>
        <table border="1">
            <thead>
                <tr>
                    <th>更新时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="tableBody">
                <!-- 这里是虚拟数据行,你可以根据需要添加更多 -->
                <tr>
                    <td>2023-10-13 10:00 ---- PM</td>
                    <td>
                        <button onclick="moveUp(this)">上移</button>
                        <button onclick="moveDown(this)">下移</button>
                        <button onclick="moveToTop(this)">置顶</button>
                        <button onclick="moveToBottom(this)">置底</button>
                        <button onclick="removeRow(this)">删除</button>
                    </td>
                </tr>
                <tr>
                    <td>2023-10-13 02:30 ---- AM</td>
                    <td>
                        <button onclick="moveUp(this)">上移</button>
                        <button onclick="moveDown(this)">下移</button>
                        <button onclick="moveToTop(this)">置顶</button>
                        <button onclick="moveToBottom(this)">置底</button>
                        <button onclick="removeRow(this)">删除</button>
                    </td>
                </tr>
            </tbody>
        </table>
    
        <button onclick="addRow()">添加数据行</button>
    
        <script>
            //添加行
            function addRow() {
                var tableBody = document.getElementById("tableBody");
                var newRow = document.createElement("tr");
                newRow.innerHTML = `
                    <td>${getCurrentTime()+'----'+Math.round(Math.random()*100+1)}</td>
                    <td>
                        <button onclick="moveUp(this)">上移</button>
                        <button onclick="moveDown(this)">下移</button>
                        <button onclick="moveToTop(this)">置顶</button>
                        <button onclick="moveToBottom(this)">置底</button>
                        <button onclick="removeRow(this)">删除</button>
                    </td>
                `;
                tableBody.appendChild(newRow);
            }
            //移除行
            function removeRow(button) {
                var row = button.parentNode.parentNode;
                row.remove();
            }
            function getCurrentTime() {
                var now = new Date();
                var year = now.getFullYear();
                var month = (now.getMonth() + 1).toString().padStart(2, '0');
                var day = now.getDate().toString().padStart(2, '0');
                var hours = now.getHours().toString().padStart(2, '0');
                var minutes = now.getMinutes().toString().padStart(2, '0');
                var time = `${year}-${month}-${day} ${hours}:${minutes}`;
                return time;
            }
        </script>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    上移

            function moveUp(button) {
                var row = button.parentNode.parentNode;
                if (row.previousElementSibling) {
                    console.log("🚀 ~ file: tabel.html:67 ~ moveUp ~ row.previousElementSibling:", row.previousElementSibling)
                    row.parentNode.insertBefore(row, row.previousElementSibling);
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    下移

            function moveDown(button) {
                var row = button.parentNode.parentNode;
                if (row.nextElementSibling) {
                    console.log("🚀 ~ file: tabel.html:74 ~ moveDown ~ row.nextElementSibling:", row.nextElementSibling)
                    row.parentNode.insertBefore(row.nextElementSibling, row);
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    置顶

            function moveToTop(button) {
                var row = button.parentNode.parentNode;
                var tableBody = row.parentNode;
                tableBody.insertBefore(row, tableBody.firstElementChild);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    置底

            function moveToBottom(button) {
                var row = button.parentNode.parentNode;
                var tableBody = row.parentNode;
                tableBody.appendChild(row);
            }
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    单面PCB布线阻抗的工程设计
    RF优化问题分析和处理
    APK 签名 v1 v2 步骤
    React 从入门到实战 一一开发环境基础搭建(小白篇)
    【通信原理】第三章 -- 随机过程[补充]
    java计算机毕业设计高校后勤保修系统MyBatis+系统+LW文档+源码+调试部署
    Pytest----如何通过pytest.ini配置不显示告警或将告警报错
    使用c#强大的表达式树实现对象的深克隆之解决循环引用的问题
    dayjs 获取一周日期 MM.DD 判断今天日期以文字格式显示(场景积分积分签到)
    SSM学习36:编程思想总结(重点)
  • 原文地址:https://blog.csdn.net/weixin_40808668/article/details/133819609