代办事项

代办事项

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代办事项</title>
    <link rel="stylesheet" href="index.css">
</head>
<body>
     <section>
        <input type="text">
        <input type="button" value="添加">
    </section>
    <section>
        <h3>代办事项</h3>
        <ol>
            <li>01</li>
            <li>02</li>
            <li>03</li>
        </ol>
    </section>
    <section>
        <input type="button" id="allItem" value="全部">
        <input type="button" id="finish" value="已完成">
        <input type="button" id="unfinish" value="未完成">
    </section>
    <script src="index.js"></script>
</body>
</html>

index.css

section {
    margin-left: 100px;
}
input[type="text"] {
    outline: none;
}
.delLi {
    color: #f00;
    text-decoration: line-through;
}

index.js

let secEles = document.getElementsByTagName("section");
let olEle = document.getElementsByTagName("ol")[0];
let arrLi = [...olEle.children];

let input1Eles = [...secEles[0].children];
input1Eles[1].addEventListener("click", () => {
    createEle(input1Eles[0].value);
    input1Eles[0].value = "";
});

olEle.addEventListener("click", e => {
    let event = e || window.event;
    if (event.target.localName == "li") {
        event.target.classList.toggle("delLi");
    }
});

secEles[2].addEventListener("click", e => {
    let event = e || window.event;
    switch (event.target.value) {
        case "全部":
            addEle(arrLi);
            break;
        case "已完成":
            addEle(arrLi.filter(item => item.className == "delLi"));
            break;
        case "未完成":
            addEle(arrLi.filter(item => item.className == ""));
            break;
    }
});

// 添加输入的待办事项
function createEle(inputTxt) {
    let liEle = document.createElement("li");
    liEle.innerText = inputTxt;
    olEle.appendChild(liEle);
    arrLi.push(liEle);
}

// 添加事项
function addEle(arr) {
    olEle.innerHTML = "";
    arr.map(item => olEle.appendChild(item));
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页