js实现奇妙清单

简易事件待办程序,按下回车即添加事件,勾选方框即跳至完成或进行的事件。纯js原生代码
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件待办</title>
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_1733615_nbs0ssr5ec.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

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

        .w {
            width: 1200px;
            margin: 0 auto;
            position: relative;
        }

        .top p {
            font-size: 40px;
            color: white;
            line-height: 50px;
            padding: 18px 0;
            margin-left: 20px;
        }

        .top {
            background-color: rgb(42, 42, 42);
        }

        .top input {
            float: right;
        }

        .top .input {
            width: 500px;
            height: 40px;
            border: 1px solid black;
            position: absolute;
            top: 21px;
            right: 50px;
            background-color: white;
            border-radius: 9px;
            box-shadow: 2px 2px 8px 1px;
        }

        .top .input input {
            width: 500px;
            height: 40px;
            border: 0;
            background-color: rgba(0, 0, 0, 0);
            outline: none;
            text-indent: 15px;
            font-size: 20px;
        }

        .title {
            font-size: 40px;
            margin-top: 30px;
        }

        .title p {
            margin-left: 20px;
            margin-bottom: 35px;
        }

        .finish {
            margin-top: 40px;
        }

        .num {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: rgb(164, 219, 159);
            position: absolute;
            top: 0;
            right: 20px;
            line-height: 40px;
            text-align: center;
            font-size: 25px;
        }

        .clearEvery {
            font-size: 24px;
            color: rgb(109, 109, 109);
            margin: 50px auto;
            width: 60px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            cursor: pointer;
        }

        .active {
            height: 55px;
            border-left: 8px solid rgb(42, 207, 130);
            border-radius: 6px;
            background-color: white;
            line-height: 55px;
            font-size: 30px;
            text-indent: 20px;
            margin-bottom: 15px;
        }

        .title i {
            font-size: 30px;
            cursor: pointer;
        }

        .title .del {
            float: right;
            margin-right: 20px;
            font-size: 15px;
            cursor: pointer;
        }

        .finish .finishEvent {
            height: 55px;
            border-left: 8px solid rgb(124, 124, 124);
            border-radius: 6px;
            background-color: rgb(219, 219, 219);
            line-height: 55px;
            font-size: 30px;
            text-indent: 20px;
            margin-bottom: 15px;
            color: rgb(97, 97, 97);
        }
    </style>
</head>

<body>
    <div class="top">
        <div class="w">
            <p>ToDoList</p>
            <div class="input">
                <input type="text" placeholder="添加ToDo">
            </div>
        </div>
    </div>
    <div class="title">
        <div class="w">
            <p>正在进行</p>
            <p class="num count">0</p>
            <ul class="newEvent">
                <!-- <li class="active"><i class="iconfont icon-xuanzekuang"></i><span class="new1">吃饭</span><span
                        class="del">删除</span></li> -->
            </ul>
        </div>
    </div>
    <div class="title">
        <div class="w">
            <p>已经完成</p>
            <p class="num count2">0</p>
            <ul class="finish">
                <!-- <li class="finishEvent"><i class="iconfont icon-xuanzekuanghou"></i><span class="new1">吃饭</span><span
                        class="del">删除</span></li> -->
            </ul>
        </div>
    </div>
    <div class="clearEvery">clear</div>
</body>
<script>
    function ToDo() {
        this.input = document.querySelector('input');
        this.i = 1;
        this.reg = /^\s+$/;//判断是否为空格字符串
        this.newEvent = document.querySelector('.title .newEvent');
        this.countnum = document.querySelector('.title .count');
        this.clearBtn = document.querySelector('.clearEvery');
        this.finish = document.querySelector('.finish');
        this.countnum2 = document.querySelector('.title .count2');
    }

    ToDo.prototype.init = function () {
        this.click();
        this.clearBtn1();
        this.clearBtn2();
        this.clearEvery();
        window.onkeydown = (ev) => {
            var ev = ev || window.event;
            if (ev.keyCode === 13) {//判断键盘事件是否为按下空格
                if (this.input.value !== '') {//判断input表单中是否输入了内容
                    if (this.reg.test(this.input.value)) {//判断init中的正则 为拦截输入为空格字符
                        console.log(`含有空格,已拦截输出`);
                        this.clear();
                    } else {
                        this.addEvent();
                        this.clear();
                        let num = this.i++;
                        console.log(`成功输出${num}次`);

                        this.clearBtn1();//删除按钮
                        this.clearBtn2();
                    }
                } else {
                    throw new Error('表单内容不能为空');
                }
            }
        }
    }

    ToDo.prototype.addEvent = function () {//添加事件
        let li = document.createElement('li');
        let lis = document.querySelectorAll('.title .newEvent li');
        let index = document.querySelectorAll('.title .newEvent .active i');
        if (index.length > 0) {//判断当前的列表插入问题,后添加的在上面
            this.newEvent.insertBefore(li, lis[0]);
        } else {
            this.newEvent.appendChild(li);
        }
        li.className = 'active';
        li.innerHTML = `
        <i class="iconfont icon-xuanzekuang"></i>
        <span>${this.input.value}</span>
        <span class="del">删除</span>`;
        this.click();
        this.count();
    }

    ToDo.prototype.click = function () {//正在进行的点击事件
        let _this = this;
        let index = document.querySelectorAll('.title .newEvent .active i');
        for (let i = 0; i < index.length; i++) {
            index[i].onclick = function () {
                // this.parentNode.children[1].innerText
                let li = document.createElement('li');
                let lis = document.querySelectorAll('.title .finish li');
                let index = document.querySelectorAll('.title .finish .finishEvent i');

                if (index.length > 0) {//判断当前的列表插入问题,后添加的在上面
                    _this.finish.insertBefore(li, lis[0]);
                } else {
                    _this.finish.appendChild(li);
                }
                li.className = 'finishEvent';
                li.innerHTML = `
                <i class="iconfont icon-xuanzekuanghou"></i>
                <span>${this.parentNode.children[1].innerText}</span>
                <span class="del">删除</span>`;

                _this.newEvent.removeChild(this.parentNode);

                _this.count();
                _this.finishCount();

                _this.finishClick();
                _this.clearBtn1();
                _this.clearBtn2();
            }
        }
    }

    ToDo.prototype.finishClick = function () {
        let _this = this;
        let index = document.querySelectorAll('.title .finish .finishEvent i');
        for (let i = 0; i < index.length; i++) {
            index[i].onclick = function () {
                // this.parentNode.children[1].innerText
                let li = document.createElement('li');
                let lis = document.querySelectorAll('.title .newEvent li');
                let index = document.querySelectorAll('.title .newEvent .active i');

                if (index.length > 0) {//判断当前的列表插入问题,后添加的在上面
                    _this.newEvent.insertBefore(li, lis[0]);
                } else {
                    _this.newEvent.appendChild(li);
                }
                li.className = 'active';
                li.innerHTML = `
                <i class="iconfont icon-xuanzekuang"></i>
                <span>${this.parentNode.children[1].innerText}</span>
                <span class="del">删除</span>`;

                _this.finish.removeChild(this.parentNode);

                _this.count();
                _this.finishCount();

                _this.click();
                _this.clearBtn1();
            }
        }
    }

    ToDo.prototype.clearBtn1 = function () {
        let _this = this;
        let clearBtn = document.querySelectorAll('.title .newEvent .del');

        for (let i = 0; i < clearBtn.length; i++) {
            clearBtn[i].onclick = function () {
                _this.newEvent.removeChild(this.parentNode);
                _this.count();
            }
        }
    }

    ToDo.prototype.clearBtn2 = function () {
        let _this = this;
        let clearBtn = document.querySelectorAll('.title .finish .del');

        for (let i = 0; i < clearBtn.length; i++) {
            clearBtn[i].onclick = function () {
                _this.finish.removeChild(this.parentNode);
                _this.finishCount();
            }
        }
    }

    ToDo.prototype.count = function () {//进行计数
        this.countnum.innerHTML = document.querySelectorAll('.title .newEvent li').length;
    }

    ToDo.prototype.finishCount = function () {//已完成计数
        this.countnum2.innerHTML = document.querySelectorAll('.title .finish li').length;
    }

    ToDo.prototype.clear = function () {
        this.input.value = '';
    }

    ToDo.prototype.clearEvery = function () {
        let _this = this;
        this.clearBtn.onclick = () => {
            if (confirm('点击"确定"将清空全部列表')) {
                let lis = document.querySelectorAll('.title .newEvent li');
                for (let i = 0; i < lis.length; i++) {
                    _this.newEvent.removeChild(_this.newEvent.children[0]);
                }
                _this.count();
                let lis2 = document.querySelectorAll('.title .finish li');
                for (let i = 0; i < lis2.length; i++) {
                    _this.finish.removeChild(_this.finish.children[0]);
                }
                _this.finishCount();
            }
        }
    }

    new ToDo().init();
</script>

</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335