• ToDoList——原生JavaScript(超详细)


    目录

    一、效果展示

    二、ToDoList——css样式

    三、ToDoList——html内容

    四、ToDoList——JavaScript部分   


    一、效果展示

    二、ToDoList——css样式

    由于系统限制,所以只能把css样式代码拿出来,有点丑,别介意哦!!!

    body {
        margin: 0;
        padding: 0;
        font-size: 16px;
        background: #CDCDCD;
    }
    header {
        height: 50px;
        background: #333;
        background: rgba(47, 47, 47, 0.98);
    }
    section {
        margin: 0 auto;
    }
    label {
        float: left;
        width: 100px;
        line-height: 50px;
        color: #DDD;
        font-size: 24px;
        cursor: pointer;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    }
    header input {
        float: right;
        width: 60%;
        height: 24px;
        margin-top: 12px;
        text-indent: 10px;
        border-radius: 5px;
        box-shadow: 0 1px 0 rgba(255, 255, 255, 0.24), 0 1px 6px rgba(0, 0, 0, 0.45) inset;
        border: none
    }
    input:focus {
        outline-width: 0
    }
    h2 {
        position: relative;
    }
    span {
        position: absolute;
        top: 2px;
        right: 5px;
        display: inline-block;
        padding: 0 5px;
        height: 20px;
        border-radius: 20px;
        background: #E6E6FA;
        line-height: 22px;
        text-align: center;
        color: #666;
        font-size: 14px;
    }

    ol,
    ul {
        padding: 0;
        list-style: none;
    }
    li input {
        position: absolute;
        top: 2px;
        left: 10px;
        width: 22px;
        height: 22px;
        cursor: pointer;
    }
    p {
        margin: 0;
    }
    li p input {
        top: 3px;
        left: 40px;
        width: 70%;
        height: 20px;
        line-height: 14px;
        text-indent: 5px;
        font-size: 14px;
    }
    li {
        height: 32px;
        line-height: 32px;
        background: #fff;
        position: relative;
        margin-bottom: 10px;
        padding: 0 45px;
        border-radius: 3px;
        border-left: 5px solid #629A9C;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
    }
    ol li {
        cursor: move;
    }
    ul li {
        border-left: 5px solid #999;
        opacity: 0.5;
    }
    li a {
        position: absolute;
        top: 2px;
        right: 5px;
        display: inline-block;
        width: 14px;
        height: 12px;
        border-radius: 14px;
        border: 6px double #FFF;
        background: #CCC;
        line-height: 14px;
        text-align: center;
        color: #FFF;
        font-weight: bold;
        font-size: 14px;
        cursor: pointer;
    }

    footer {
        color: #666;
        font-size: 14px;
        text-align: center;
    }
    footer a {
        color: #666;
        text-decoration: none;
        color: #999;
    }
    @media screen and (max-device-width: 620px) {
        section {
            width: 96%;
            padding: 0 2%;
        }
    }
    @media screen and (min-width: 620px) {
        section {
            width: 600px;
            padding: 0 10px;
        }
    }

    三、ToDoList——html内容

     

    1. html>
    2. <html>
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    6. <title>ToDoList—最简单的待办事项列表title>
    7. <meta name="description" content="ToDoList无须注册即可使用,数据存储在用户浏览器的html5本地数据库里,是最简单最安全的待办事项列表应用!" />
    8. <link rel="stylesheet" href="./index.css">
    9. head>
    10. <body>
    11. <header>
    12. <section>
    13. <form action="" id="form" onclick="">
    14. <label for="title">ToDoListlabel>
    15. <input type="text" id="title" name="title" placeholder="添加ToDo" required="required"
    16. autocomplete="off" />
    17. form>
    18. section>
    19. header>
    20. <section>
    21. <h2>正在进行 <span id="todocount">span>h2>
    22. <ol id="todolist" class="demo-box">
    23. ol>
    24. <h2>已经完成 <span id="donecount">span>h2>
    25. <ul id="donelist">
    26. ul>
    27. section>
    28. <footer>
    29. Copyright © 2014 todolist.cn <a href="javascript:clear();">cleara>
    30. footer>
    31. <script type="text/javascript" src="./index.js">script>
    32. body>
    33. html>

     

    四、ToDoList——JavaScript部分
       

    1. //获取元素
    2. var title = document.querySelector('#title');
    3. var ol = document.querySelector('#todolist');
    4. var ul = document.querySelector('#donelist');
    5. load();
    6. //键盘点击事件
    7. title.addEventListener('keydown', function (e) {
    8. var arr = getData(); //再获取数据的基础上拿到数组
    9. if (e.keyCode == 13) {
    10. var todo = {
    11. title: title.value, //把表单值赋给title
    12. done: false, //后面上下数据交换使用
    13. }
    14. arr.push(todo); //把数据放到数组中
    15. // console.log(arr);
    16. saveData(arr); //存储数据
    17. var form = document.querySelector('#form'); //让表单返回默认值
    18. form.reset();
    19. load();
    20. }
    21. })
    22. //获取数据
    23. function getData() {
    24. var data = window.localStorage.getItem('todo'); //得到todo中存储的数据
    25. if (data != null) {
    26. return JSON.parse(data); //数据转化类数组,对象
    27. } else {
    28. return [];
    29. }
    30. }
    31. //存储数据
    32. function saveData(tdlist) {
    33. window.localStorage.setItem('todo', JSON.stringify(tdlist)); //存储数据和将数组转化为字符串
    34. }
    35. //显示数据
    36. function load() {
    37. var todoCount = document.querySelector('#todocount');
    38. var doneCount = document.querySelector('#donecount');
    39. var todocount = 0;
    40. var donecount = 0;
    41. //删除节点
    42. var olchilds = ol.childNodes; //ol全部子节点
    43. for (var i = olchilds.length - 1; i >= 0; i--) {
    44. ol.removeChild(olchilds[i]);
    45. }
    46. var ulchilds = ul.childNodes;
    47. for (var i = ulchilds.length - 1; i >= 0; i--) {
    48. ul.removeChild(ulchilds[i]);
    49. }
    50. var data = getData(); //获取之前数据
    51. data.forEach((item, index) => { //遍历data
    52. var li = document.createElement('li'); //创建li节点
    53. li.innerHTML = `

      ${index}">${item.title}

      ${index}
      ">` //通过拼接填写内容
    54. if (item.done) { //为实现数据数量
    55. ul.insertBefore(li, ul.children[0]);//在ul前添加数据让输入数据从第一个开始输入
    56. li.children[0].checked = 'checked' //ul里的li默认选中
    57. donecount++; //每执行一次加一
    58. } else {
    59. ol.insertBefore(li, ol.children[0]);//在ol前添加数据让输入数据从第一个开始输入
    60. todocount++; //每执行一次加一
    61. }
    62. });
    63. todoCount.innerText = todocount; //将次数输出到页面
    64. doneCount.innerText = donecount;
    65. }
    66. //编辑数据
    67. function edit() {
    68. ol.addEventListener('click', function (e) { //表单内容的输入点击事件
    69. var p = e.target; //e.target点击结果为每个的属性(INPUT,P...)
    70. // console.log(p.nodeName);
    71. if (p.nodeName == 'P') { //寻找P的点击的元素
    72. p.innerHTML = `${p.innerText}" id="input${p.id}"/>`; //给P添加内容
    73. // p.children[0].select();
    74. p.children[0].addEventListener('blur', function () { //失焦
    75. var data = getData(); //获取数据
    76. var i = p.id.substring(1); //截取字符串 原p1 => p
    77. data[i].title = this.value; //赋值
    78. saveData(data); //存储
    79. load(); //页面显示
    80. })
    81. }
    82. })
    83. }
    84. edit();
    85. //删除数据
    86. function remove() {
    87. ol.addEventListener('click', function (e) { //ol的点击事件
    88. var a = e.target; //与上部分同理
    89. if (a.nodeName == 'A') { //选取A
    90. var i = a.id;
    91. var data = getData(); //拿取数据
    92. data.splice(i, 1); //删除数据
    93. saveData(data); //存储
    94. load(); //页面显示
    95. }
    96. })
    97. }
    98. remove();
    99. //切换数据
    100. function change() {
    101. ol.addEventListener('click', function (e) { //点击input表单前面的选项
    102. var input = e.target;
    103. if (input.nodeName == 'INPUT') {
    104. var i = input.nextElementSibling.nextElementSibling.id; //获取点击的id
    105. var data = getData(); //拿取数据
    106. data[i].done = true; //切换
    107. saveData(data);
    108. load();
    109. }
    110. })
    111. ul.addEventListener('click', function (e) {
    112. var input = e.target;
    113. if (input.nodeName == 'INPUT') {
    114. var i = input.nextElementSibling.nextElementSibling.id;
    115. var data = getData();
    116. data[i].done = false;
    117. saveData(data);
    118. load();
    119. }
    120. })
    121. }
    122. change();

     

     

  • 相关阅读:
    2024 CISCN WEB 部分wp
    SQL数据库添加新账号,只操作指定数据库
    [极客大挑战 2019]RCE ME 1
    申请软件著作权的流程
    B+tree - B+树深度解析+C语言实现+opencv绘图助解
    【SLAM中的问题相关解决方案】
    分布式多主关系数据库的底线业务优势
    js input手机号正则限制11位数字
    LeetCode 2562. 找出数组的串联值【数组,相向双指针】1259
    Stable Diffusion - 配置 WebUI 升级至 v1.6.0 版本与 VirtualENV 环境配置
  • 原文地址:https://blog.csdn.net/qq_65715980/article/details/126122507