js實現(xiàn)奇妙清單

簡易事件待辦程序助析,按下回車即添加事件掘譬,勾選方框即跳至完成或進行的事件。純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" >
    <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 () {//進行計數(shù)
        this.countnum.innerHTML = document.querySelectorAll('.title .newEvent li').length;
    }

    ToDo.prototype.finishCount = function () {//已完成計數(shù)
        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>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市芙盘,隨后出現(xiàn)的幾起案子驯用,更是在濱河造成了極大的恐慌,老刑警劉巖儒老,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蝴乔,死亡現(xiàn)場離奇詭異,居然都是意外死亡驮樊,警方通過查閱死者的電腦和手機薇正,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門片酝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人挖腰,你說我怎么就攤上這事雕沿。” “怎么了曙聂?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵晦炊,是天一觀的道長。 經常有香客問我宁脊,道長断国,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任榆苞,我火速辦了婚禮稳衬,結果婚禮上,老公的妹妹穿的比我還像新娘坐漏。我一直安慰自己薄疚,他們只是感情好,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布赊琳。 她就那樣靜靜地躺著街夭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪躏筏。 梳的紋絲不亂的頭發(fā)上板丽,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天,我揣著相機與錄音趁尼,去河邊找鬼埃碱。 笑死,一個胖子當著我的面吹牛酥泞,可吹牛的內容都是我干的砚殿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼芝囤,長吁一口氣:“原來是場噩夢啊……” “哼似炎!你這毒婦竟也來了?” 一聲冷哼從身側響起悯姊,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤名党,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后挠轴,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體传睹,經...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年岸晦,在試婚紗的時候發(fā)現(xiàn)自己被綠了欧啤。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片睛藻。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖邢隧,靈堂內的尸體忽然破棺而出店印,到底是詐尸還是另有隱情,我是刑警寧澤倒慧,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布按摘,位于F島的核電站,受9級特大地震影響纫谅,放射性物質發(fā)生泄漏炫贤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一付秕、第九天 我趴在偏房一處隱蔽的房頂上張望兰珍。 院中可真熱鬧,春花似錦询吴、人聲如沸掠河。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽唠摹。三九已至,卻和暖如春奉瘤,著一層夾襖步出監(jiān)牢的瞬間跃闹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工毛好, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人苛秕。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓肌访,卻偏偏與公主長得像,于是被迫代替她去往敵國和親艇劫。 傳聞我的和親對象是個殘疾皇子吼驶,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354