原创javascript-todos(超全易理解)

 

 

 

<body>
    <div id="container">
        <div class="title">todos</div>
        <div class="enteradd">
            <input id="input" type="text" placeholder="what needs to be done?" aria-label="New task input">
            <button id="add" aria-label="Add task">add</button>
        </div>
        <div id="article" aria-live="polite">
        </div>
        <div class="footer">
            <button id="allchecked" aria-label="Toggle all tasks">all checked</button>
            <button id="allcompleted" aria-label="Complete all tasks">completed ✔️</button>
            <button id="alldeleted" aria-label="Delete all checked tasks">deleted ❌</button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const input = document.getElementById('input');
            const add = document.getElementById('add');
            const article = document.getElementById('article');

            // 添加任务的函数
            function addTask() {
                if (input.value.trim() === '') {
                    alert('内容不能为空');
                    return;
                }
                let newJob = document.createElement('div');
                newJob.className = 'job';
                newJob.innerHTML = `
                    <input type="checkbox" class="checkbox" aria-label="Task checkbox">
                    <span class="jobname">${input.value}</span>
                    <button class="delete" aria-label="Delete task">delete</button>`;
                article.appendChild(newJob);
                input.value = ''; // 清空输入框
            }

            // 点击添加按钮时添加任务
            add.addEventListener('click', addTask);

            // 按下回车键时添加任务
            input.addEventListener('keypress', (e) => {
                if (e.key === 'Enter') {
                    addTask();
                }
            });
        });

        document.getElementById('container').addEventListener('click', function (e) {
            if (e.target.classList.contains('checkbox')) {
                let thejob = e.target.parentNode.querySelector('span');
                if (e.target.checked) {
                    thejob.classList.add('done');
                } else {
                    thejob.classList.remove('done');
                }
            }
            if (e.target.classList.contains('delete')) {
                e.target.parentNode.remove();
            }
            if (e.target.id === 'allchecked') {
                let allCheckboxes = document.querySelectorAll('.checkbox');
                let allChecked = Array.from(allCheckboxes).every(checkbox => checkbox.checked);
                allCheckboxes.forEach(item => item.checked = !allChecked);
            }
            if (e.target.id === 'alldeleted') {
                let allCheckboxes = document.querySelectorAll('.checkbox');
                allCheckboxes.forEach(checkbox => {
                    if (checkbox.checked) {
                        checkbox.parentNode.remove();
                    }
                });
            }
            if (e.target.id === 'allcompleted') {
                let allCheckboxes = document.querySelectorAll('.checkbox');
                allCheckboxes.forEach(checkbox => {
                    if (checkbox.checked) {
                        checkbox.nextElementSibling.classList.add('done');
                    }
                });
            }
        });
    </script>

</body>
  <style>
        * {
            padding: 0;
            margin: 0;
            outline: none;
            border: none;
            list-style: none;
        }

        #container {
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 500px;
            height: 500px;
            border-radius: 10px;
            box-shadow: 5px 5px 10px #aaa;
            display: grid;
            grid-template-rows: 140px 40px 1fr 50px;
            overflow-y: auto; /* 允许滚动条 */
        }

        .title {
            color: blue;
            font-size: 45px;
            background-color: #e3e3e3;
            font-weight: bold;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .enteradd {
            display: flex;
            border-bottom: 3px solid #e4e0e0;

            #input {
                flex: 7;
            }

            #input::placeholder {
                font-size: 20px;
                color: #cacaca;
                font-style: italic;
            }

            #add {
                flex: 1;
                color: rgb(2, 6, 37);
                font-size: 20px;
                font-weight: bold;
            }
        }

        #article {
            .job {
                display: flex;
                align-items: center;
                margin: 10px;
                position: relative;
                border-bottom: 2px solid #ebe9e9;
                color: rgb(0, 47, 141);
                font-size: 20px;
                font-weight: 700;

                .checkbox {
                    appearance: none;
                    width: 20px;
                    height: 20px;
                    border-radius: 50%;
                    border: 1px solid #aaa;
                    position: relative;
                    margin: 10px;
                }

                .checkbox:checked {
                    border: 1px solid green;
                }

                .checkbox:checked::after {
                    content: '✔️';
                    font-weight: bold;
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                }

                .delete {
                    width: 60px;
                    position: absolute;
                    right: 30px;
                    color: red;
                    font-size: 15px;
                    font-weight: 600;
                    box-shadow: 2px 2px 5px #cacaca;
                }
            }

            .job:hover {
                background-color: rgba(145, 195, 248, 0.5);
                box-shadow: 2px 2px 5px #cacaca;
            }
        }

        .footer {
            display: grid;
            position: sticky;
            bottom: 0;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 15px;
            margin: 0 10px;

            button {
                height: 35px;
                color: rgb(255, 254, 254);
                font-size: 15px;
                font-weight: 600;
                box-shadow: 2px 2px 5px #cacaca;
                line-height: 20px;
                border-radius: 5px;
                cursor: pointer;
            }

            #allchecked {
                background-color: rgb(35, 43, 198);
            }

            #allcompleted {
                background-color: rgb(11, 144, 26);
            }

            #alldeleted {
                background-color: rgb(170, 3, 50);
            }
        }

        .done {
            text-decoration: line-through;
            color: #6d6d6d;
        }
    </style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值