• todoList清单(HTML+CSS+JavaScript)


       🌏个人博客主页:

    前言: 前段时间学习了JavaScript,然后写了一个todoList小项目,现在和大家分享一下我的清单以及如何实现的,希望对大家有所帮助

    🔥🔥🔥文章专题todoList清单

    😽感谢大家的点赞👍收藏⭐️评论✍您的一键三连是我更新的动力 💓 


     清单效果:

    页面展示: 

     tab-工作-tab-生活-tab-学习-tab-健康:

    功能介绍和代码详细解释:

    我一共写了4个页面,但是每个页面都是基本一样的,所以我通过第一个页面向大家展示我的功能都有哪些并且是如何实现的

    大体思路: 

    现在我来介绍一下我大体是如何实现的,首先我创造两个二维数组(一个arrList1数组储存我的未完成内容,一个arrList2数组储存我的已完成内容),还有一个全局变量   dataId,dataId在这里的作用非常大,dataId通过tab点击可以改变我的dataId的内容,然后显示我的第几个页面和改变我的二维数组的第一个下标的值(四个页面我都设置了相class=“note”属性,所以可以通过tab关联的dataId显示我的第几个note和转换我的第几个数组

    提示:(第一个页面是我的arrList1[0]和arrList2[0])

    每一个页面的内容的存储都相当于两个一维数组

     1 全局dataId和二维数组显示:
    1. // 判断到第几个note
    2. let dataId = 1
    3. //定义数组
    4. if (!this.localStorage.getItem('arr')) {
    5. localStorage.setItem('arr', "[[], [], [], []]")
    6. } else if (!this.localStorage.getItem('arr1')) {
    7. localStorage.setItem('arr1', "[[], [], [], []]")
    8. }
    9. //我的未完成数组
    10. const arrList1 = JSON.parse(localStorage.getItem('arr'))
    11. //我的已完成数组
    12. const arrList2 = JSON.parse(localStorage.getItem('arr1'))
    2 更新未完成和已完成:

    因为更新已完成和未完成相似 ,所以下面我就通过更新未完成来和大家讲解,关于更新添加,我是通过将我的本地存储解析成数组然后遍历将这些内容通过join返回成一个字符串,添加到我的un_ul里面,最终显示在页面当中

    1. // 更新未完成
    2. function renderUnfinished() {
    3. let un_ul = document.querySelector(`.note:nth-child(${dataId}) .uul-unfinished`);
    4. const listItems = JSON.parse(localStorage.getItem('arr'))[dataId - 1].map((taskText, index) => {
    5. return `
  • ${taskText}
  • 定时
  • ${index}">
  • `
  • }).join('')
  • console.log(JSON.parse(localStorage.getItem('arr'))[dataId - 1])
  • un_ul.innerHTML = listItems
  • }
  • // 更新已完成
  • function renderFinished() {
  • let ul = document.querySelector(`.note:nth-child(${dataId}) .uul-finished`)
  • const listItems = JSON.parse(localStorage.getItem('arr1'))[dataId - 1].map((taskText, index) => {
  • return `
  • ${taskText}
  • 定时
  • ${index}">
  • `
  • }).join('')
  • ul.innerHTML = listItems
  • }
  • 3 input输入框回车输入内容:

     第一步获得我的input标签,然后我给我的input标签添加键盘事件监听,如果监听到点击键盘的回车Enter键,并且我的内容不为空,然后就添加给我的数组和我的本地存储,并且通过我的 renderUnfinished方法进行显示,最后将我的input标签内容变为空

    1. // 如果按下了回车事件
    2. let inputTask = document.querySelector('#task')
    3. inputTask.addEventListener('keydown', function (e) {
    4. if (e.key === 'Enter') {
    5. let newTaskText = inputTask.value.trim()
    6. if (newTaskText !== '') {
    7. arrList1[dataId - 1].push(newTaskText)
    8. localStorage.setItem('arr', JSON.stringify(arrList1))
    9. //更新未完成事件界面
    10. renderUnfinished()
    11. //将input设为空
    12. inputTask.value = ''
    13. }
    14. }
    15. })
    4 点击删除未完成:

     首先给父级元素添加点击事件,通过dataID来获得是第几个li标签然后删除该位置的数组内容(dataID和一维数组是关联的)通过dataID可以当中数组下标得到内容所以可以直接使用dataID进行数组操作,定位到第几个内容然后直接进行删除操作,然后改变本地存储,最后进行遍历显示

    1. // 删除未完成
    2. //un_ul是存储内容li的父级ul标签,这里使用冒泡来获得li
    3. un_ul.addEventListener('click', function (e) {
    4. //判断点击的是否是delete小图标
    5. if (e.target.classList.contains('delete')) {
    6. let dataID = parseInt(e.target.dataset.id)
    7. arrList1[dataId - 1].splice(dataID, 1)
    8. console.log(arrList1)
    9. //往本地存储添加相同密匙的内容只会覆盖
    10. localStorage.setItem('arr', JSON.stringify(arrList1))
    11. renderUnfinished()
    12. }
    13. })
    5 点击删除已完成:

    这里的操作和上面的删除未完成操作一样,只是获取的父级元素不一样

    1. // 删除已完成
    2. ul.addEventListener('click', function (e) {
    3. if (e.target.classList.contains('delete')) {
    4. let dataID = parseInt(e.target.dataset.id)
    5. // console.log(dataID)
    6. arrList2[dataId - 1].splice(dataID, 1)
    7. localStorage.setItem('arr1', JSON.stringify(arrList2))
    8. renderFinished()
    9. }
    10. })
    6 删除全部未完成:

    这个就是点击全部已完成的按钮,然后将数组和本地存储清空,最后进行更新

    1. //删除全部已完成
    2. text1.addEventListener('click', function (e) {
    3. if (e.target.tagName === 'SPAN') {
    4. arrList2[dataId - 1] = []
    5. localStorage.setItem('arr1', JSON.stringify(arrList2))
    6. renderFinished()
    7. }
    8. })
    7 点击圆形图标未完成变成已完成:

    这个就是点击li标签的第一个小图标,然后使得该内容从未完成变成已完成,内部操作就是点击该图标,通过dataName为下标来获得该处的内容,然后将该内容添加到已完成的数组里面,最后将该内容在未完成里面删除,最后修改本地存储,然后进行更新

    1. // 点击未完成按钮任务变成完成
    2. un_ul.addEventListener('click', function (e) {
    3. if (e.target.classList.contains('finished')) {
    4. let dataName = parseInt(e.target.dataset.name);
    5. arrList2[dataId - 1].push(arrList1[dataId - 1][dataName]);
    6. arrList1[dataId - 1].splice(dataName, 1);
    7. localStorage.setItem('arr1', JSON.stringify(arrList2));
    8. localStorage.setItem('arr', JSON.stringify(arrList1));
    9. renderFinished();
    10. renderUnfinished();
    11. }
    12. })

    8 时间提醒:

    这个是我的select标签里面的option进行的change 才会触发事件监听,虽然还是冒泡,但是在if语句里面进行了判断,开始获得事件,然后添加了计时器将时间每秒减一次,当时间为0时重复上面的未完成功能到已完成的操作

    1. // 时间提醒
    2. un_ul.addEventListener('change', function (e) {
    3. if (e.target && e.target.classList.contains('timeNum')) {
    4. let timer = +e.target.value;
    5. let time = setInterval(function () {
    6. timer--;
    7. let dataName = parseInt(e.target.dataset.name);
    8. if (timer === 0) {
    9. arrList2[dataId - 1].push(arrList1[dataId - 1][dataName]);
    10. arrList1[dataId - 1].splice(dataName, 1);
    11. localStorage.setItem('arr', JSON.stringify(arrList1));
    12. localStorage.setItem('arr1', JSON.stringify(arrList2));
    13. renderFinished();
    14. renderUnfinished();
    15. alert('滴滴,时间到');
    16. clearInterval(time);
    17. }
    18. }, 1000)
    19. }
    20. })
    9 计时器

    获得当前时间进行显示 ,然后通过计时器每秒更改一次时间

    1. // 显示当前时间
    2. function showTime() {
    3. let date = new Date()
    4. // 年月日
    5. let year = date.getFullYear()
    6. let month = date.getMonth() + 1
    7. let day = date.getDate()
    8. // 时分秒
    9. let hour = date.getHours()
    10. hour = hour < 10 ? '0' + hour : hour
    11. let minute = date.getMinutes()
    12. minute = minute < 10 ? '0' + minute : minute
    13. let second = date.getSeconds()
    14. second = second < 10 ? '0' + second : second
    15. // 显示
    16. let element = document.getElementById('date')
    17. element.innerHTML = '

      ' + year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '

      '
    18. }
    19. let ShowTime = window.setInterval(showTime, 1000)

     全部代码展示:

    HTML:
    1. <body>
    2. <p id="date">p>
    3. <div class="entire">
    4. <div class="title"><img class="pic" src="./image/title.png"><img class="pic1" src="./image/bianqian.png">
    5. div>
    6. <div class="header">
    7. <input id="task" type="text" placeholder="添加请按回车键">
    8. div>
    9. <div class="middle">
    10. <div class="tabs">
    11. <div class="work active" data-id="1">工作div>
    12. <div class="life" data-id="2">生活div>
    13. <div class="study" data-id="3">学习div>
    14. <div class="health" data-id="4">健康div>
    15. div>
    16. <div class="content">
    17. <div class="note active">
    18. <div class="work-unfinished">
    19. <div class="note-text">未完成div>
    20. <ul class="uul-unfinished">
    21. ul>
    22. div>
    23. <div class="work-finished">
    24. <div class="text">
    25. <div class="note-text">已完成div>
    26. <span class="deleteAll">删除全部span>
    27. <img src="./image/unfinished.png">
    28. div>
    29. <ul class="uul-finished">
    30. ul>
    31. div>
    32. div>
    33. <div class="note">
    34. <div class="life-unfinished">
    35. <div class="note-text">未完成div>
    36. <ul class="uul-unfinished">
    37. ul>
    38. div>
    39. <div class="life-finished">
    40. <div class="text">
    41. <div class="note-text">已完成div>
    42. <span class="deleteAll">删除全部span>
    43. <img src="./image/unfinished.png">
    44. div>
    45. <ul class="uul-finished">
    46. ul>
    47. div>
    48. div>
    49. <div class="note">
    50. <div class="study-unfinished">
    51. <div class="note-text">未完成div>
    52. <ul class="uul-unfinished">
    53. ul>
    54. div>
    55. <div class="study-finished">
    56. <div class="text">
    57. <div class="note-text">已完成div>
    58. <span class="deleteAll">删除全部span>
    59. <img src="./image/unfinished.png">
    60. div>
    61. <ul class="uul-finished">
    62. ul>
    63. div>
    64. div>
    65. <div class="note">
    66. <div class="health-unfinished">
    67. <div class="note-text">未完成div>
    68. <ul class="uul-unfinished">
    69. ul>
    70. div>
    71. <div class="health-finished">
    72. <div class="text">
    73. <div class="note-text">已完成div>
    74. <span class="deleteAll">删除全部span>
    75. <img src="./image/unfinished.png">
    76. div>
    77. <ul class="uul-finished">
    78. ul>
    79. div>
    80. div>
    81. div>
    82. div>
    83. div>
    84. div>
    85. <script src="./todoList.js">script>
    86. body>

    CSS:
    1. * {
    2. margin: 0;
    3. margin: 0;
    4. box-sizing: border-box;
    5. }
    6. li {
    7. list-style: none;
    8. }
    9. a {
    10. text-decoration: none;
    11. }
    12. body {
    13. background-color: #fffbfb;
    14. background-image: url(./image/bigbgc.png);
    15. height: 1300px;
    16. background-repeat: no-repeat;
    17. background-size: cover;
    18. user-select: none;
    19. }
    20. #date {
    21. text-align: center;
    22. height: 80px;
    23. font-size: 50px;
    24. color: #fff;
    25. }
    26. .entire {
    27. margin: auto;
    28. width: 600px;
    29. height: 1100px;
    30. border-radius: 45px;
    31. border: 1px solid rgb(228, 228, 255);
    32. box-shadow: 5px 5px 15px rgb(201, 198, 198), -5px -5px 10px #fff;
    33. background-color: #ffefef;
    34. /* background-image: url(./image/bgc.png);
    35. background-size: contain; */
    36. }
    37. .task-icon {
    38. cursor: pointer;
    39. font-family: 'icomoon';
    40. display: inline-block;
    41. width: 40px;
    42. height: 40px;
    43. font-size: 40px;
    44. text-align: center;
    45. border-radius: 20px;
    46. /* box-shadow: 5px 5px 5px rgb(255, 255, 255), 5px 5px 5px #fffdfd; */
    47. color: #ffffff;
    48. margin-right: 10px;
    49. margin-bottom: 9px;
    50. }
    51. .life-finished .task-icon,
    52. .life-unfinished .task-icon {
    53. color: #ffd1d1;
    54. }
    55. .health-finished .task-icon,
    56. .health-unfinished .task-icon {
    57. color: #dde6ff;
    58. }
    59. .title {
    60. margin: auto;
    61. width: 400px;
    62. height: 100px;
    63. border-radius: 40px;
    64. text-align: center;
    65. box-shadow: 5px 5px 15px rgb(201, 198, 198), -5px -5px 10px #fff;
    66. margin-bottom: 20px;
    67. }
    68. /* .title span {
    69. color: #d27171;
    70. font-size: 30px;
    71. margin-top: 5px;
    72. text-align: center;
    73. vertical-align: middle;
    74. } */
    75. .title .pic {
    76. width: 80px;
    77. height: 80px;
    78. vertical-align: middle;
    79. margin-top: 10px;
    80. margin-right: 10px;
    81. }
    82. .title .pic1 {
    83. border-radius: 12px;
    84. width: 160px;
    85. vertical-align: middle;
    86. margin-top: 10px;
    87. margin-right: 10px;
    88. }
    89. .header input {
    90. margin-left: 105px;
    91. padding-left: 15px;
    92. width: 390px;
    93. height: 55px;
    94. border-radius: 25px;
    95. border: none;
    96. outline: none;
    97. font-size: 22px;
    98. color: #e09191;
    99. background-color: #ffefef;
    100. box-shadow: 5px 5px 15px rgb(219, 218, 218) inset, -5px -5px 10px #fff inset;
    101. }
    102. input::placeholder {
    103. color: #f2b1b1;
    104. font-size: 21px;
    105. }
    106. .tabs {
    107. margin-top: 35px;
    108. display: flex;
    109. gap: 10px;
    110. border-bottom: 2px solid #f0b2b2;
    111. }
    112. .tabs div {
    113. padding: 10px 20px;
    114. background-color: #ffefef;
    115. border: 2px solid #e7b3b3;
    116. border-top-left-radius: 36px;
    117. border-top-right-radius: 36px;
    118. border-bottom: none;
    119. cursor: pointer;
    120. color: #e59898;
    121. box-shadow: 5px 5px 15px rgb(250, 241, 241) inset, -5px -5px 10px #fff inset;
    122. }
    123. .tabs .active {
    124. background-color: #ffd5d5;
    125. }
    126. .timing {
    127. width: 140px;
    128. text-align: center;
    129. }
    130. #timing-list {
    131. width: 60px;
    132. color: rgb(206, 168, 168);
    133. outline: none;
    134. border-color: rgb(228, 128, 128);
    135. border-radius: 10px;
    136. margin-top: 13px;
    137. }
    138. .content {
    139. padding-top: 15px;
    140. }
    141. .note {
    142. padding-top: 20px;
    143. margin: auto;
    144. width: 560px;
    145. height: 790px;
    146. border: 3px solid #f7dfdf;
    147. border-radius: 40px;
    148. }
    149. .note:nth-child(1) {
    150. background-color: #ffdddd;
    151. }
    152. .note:nth-child(2) {
    153. background-color: #fffefe;
    154. }
    155. .note:nth-child(2) .center-item {
    156. color: #da5d5d;
    157. }
    158. .note:nth-child(3) {
    159. background-color: #ffdddd;
    160. }
    161. .note:nth-child(3) .center-item {
    162. color: #ffffff;
    163. }
    164. .note:nth-child(4) {
    165. background-color: #f8f9ff;
    166. }
    167. .note:nth-child(4) .center-item {
    168. color: #7d9fb8;
    169. }
    170. /* 设置背景图片 */
    171. .work-unfinished,
    172. .work-finished {
    173. background-image: url(./image/bgc.png);
    174. }
    175. .life-finished,
    176. .life-unfinished {
    177. background-image: url(./image/bgc1.png);
    178. background-size: cover;
    179. }
    180. .study-unfinished,
    181. .study-finished {
    182. background-image: url(./image/bgc2.png);
    183. }
    184. .health-unfinished,
    185. .health-finished {
    186. background-image: url(./image/bgc3.png);
    187. }
    188. /* 设置完成和未完成的框的大小和样式 */
    189. .work-unfinished,
    190. .work-finished,
    191. .life-unfinished,
    192. .life-finished,
    193. .study-unfinished,
    194. .study-finished,
    195. .health-unfinished,
    196. .health-finished {
    197. padding-top: 10px;
    198. margin: auto;
    199. height: 360px;
    200. width: 510px;
    201. border-radius: 25px;
    202. border: #eadddd solid 2px;
    203. box-shadow: 5px 5px 5px rgb(223, 223, 223) inset, -5px -5px 10px #f2e8e8 inset;
    204. }
    205. /* 设置完成和未完成之间的间隔为20px */
    206. .work-unfinished,
    207. .life-unfinished,
    208. .study-unfinished,
    209. .health-unfinished {
    210. margin-bottom: 20px;
    211. }
    212. /* 设置完成和未完成的字体样式 */
    213. .note-text {
    214. margin: auto;
    215. width: 140px;
    216. height: 50px;
    217. border-radius: 22px;
    218. padding-top: 5px;
    219. color: #d58585;
    220. font-size: 26px;
    221. text-align: center;
    222. }
    223. /* 更换第三个标签主题字体颜色 */
    224. .study-unfinished .note-text,
    225. .study-finished .note-text {
    226. color: #ffffff;
    227. }
    228. /* 更换第一个标签主题颜色 */
    229. .work-finished .note-text,
    230. .work-unfinished .note-text {
    231. box-shadow: 5px 5px 5px rgb(248, 157, 157) inset, -5px -5px 10px #f2e8e8 inset;
    232. background-color: #ffdfdf;
    233. }
    234. .life-finished .note-text,
    235. .life-unfinished .note-text {
    236. box-shadow: 5px 5px 5px rgb(248, 157, 157) inset, -5px -5px 10px #f2e8e8 inset;
    237. background-color: #fff8f8;
    238. }
    239. .study-unfinished .note-text,
    240. .study-finished .note-text {
    241. box-shadow: 5px 5px 5px rgb(207, 161, 161) inset, -5px -5px 10px #f2e8e8 inset;
    242. background-color: #ffc5c5;
    243. }
    244. .health-finished .note-text,
    245. .health-unfinished .note-text {
    246. box-shadow: 5px 5px 5px #e3f1ff inset, -5px -5px 10px #dbdbdb inset;
    247. color: #d6e3ff;
    248. background-color: #ffffff;
    249. }
    250. .work-finished .note-text,
    251. .life-finished .note-text,
    252. .study-finished .note-text,
    253. .health-finished .note-text {
    254. margin-left: 185px;
    255. }
    256. .uul-unfinished,
    257. .uul-finished {
    258. margin-top: 5px;
    259. display: block;
    260. width: 510px;
    261. height: 295px;
    262. overflow-y: scroll;
    263. overflow-x: hidden;
    264. }
    265. /* 隐藏滚动条 */
    266. .uul-unfinished::-webkit-scrollbar,
    267. .uul-finished::-webkit-scrollbar {
    268. width: 0;
    269. }
    270. .uul-unfinished li,
    271. .uul-finished li {
    272. padding: 11px;
    273. margin-top: 12px;
    274. margin-left: -10px;
    275. border-radius: 30px;
    276. display: flex;
    277. height: 60px;
    278. width: 450px;
    279. text-align: center;
    280. font-size: 17px;
    281. color: #c06666;
    282. }
    283. .work-unfinished .uul-unfinished li,
    284. .work-finished .uul-finished li {
    285. box-shadow: 5px 5px 15px rgb(255, 255, 255) inset, -5px -5px 10px #f2e8e8 inset;
    286. background-color: #ffc3c3;
    287. }
    288. .life-unfinished .uul-unfinished li,
    289. .life-finished .uul-finished li {
    290. box-shadow: 5px 5px 5px rgb(255, 211, 211) inset, -5px -5px 10px #f2e8e8 inset;
    291. background-color: #ffffff;
    292. }
    293. .study-unfinished .uul-unfinished li,
    294. .study-finished .uul-finished li {
    295. box-shadow: 5px 5px 15px rgb(255, 255, 255) inset, -5px -5px 10px #f2e8e8 inset;
    296. background-color: #ffa3a3;
    297. }
    298. .health-unfinished .uul-unfinished li,
    299. .health-finished .uul-finished li {
    300. box-shadow: 5px 5px 15px rgb(255, 255, 255) inset, -5px -5px 10px #e6f8ff inset;
    301. background-color: #ffffff;
    302. }
    303. .center-item {
    304. width: 250px;
    305. height: 20px;
    306. overflow: hidden;
    307. margin-top: 10px;
    308. text-align: left;
    309. }
    310. .uul-finished .center-item {
    311. text-decoration: line-through;
    312. }
    313. /* .delete {
    314. cursor: pointer;
    315. color: #ffffff;
    316. height: 36px;
    317. width: 36px;
    318. } */
    319. /* 设置变签内容不可看 */
    320. .note {
    321. display: none;
    322. }
    323. /* 设置变迁内容可看 */
    324. .content .active {
    325. display: block;
    326. }
    327. /* 设置完成文字图片在一排 */
    328. .text {
    329. display: flex;
    330. }
    331. /*
    332. .deleteAll {
    333. cursor: pointer;
    334. background-image: url(./image/unfinished.png);
    335. width: 50px;
    336. height: 50px;
    337. } */
    338. .text img {
    339. width: 30px;
    340. height: 30px;
    341. border-radius: 15px;
    342. margin-right: 10px;
    343. margin-left: 10px;
    344. margin-top: 4px;
    345. }
    346. .text span {
    347. width: 100px;
    348. height: 40px;
    349. /* cursor: pointer;/ */
    350. text-align: center;
    351. vertical-align: middle;
    352. color: #8d2222;
    353. font-size: 20px;
    354. padding: 4px;
    355. border-radius: 15px;
    356. border: 1px solid rgb(255, 217, 142);
    357. }
    JS: 
    1. window.addEventListener('load', function () {
    2. // 判断到第几个note
    3. let dataId = 1
    4. //显示时间1
    5. showTime()
    6. //定义数组
    7. if (!this.localStorage.getItem('arr')) {
    8. localStorage.setItem('arr', "[[], [], [], []]")
    9. } else if (!this.localStorage.getItem('arr1')) {
    10. localStorage.setItem('arr1', "[[], [], [], []]")
    11. }
    12. const arrList1 = JSON.parse(localStorage.getItem('arr'))
    13. const arrList2 = JSON.parse(localStorage.getItem('arr1'))
    14. // 初始化所有标签页的任务列表
    15. for (let i = 1; i <= 4; i++) {
    16. dataId = i
    17. renderUnfinished()
    18. renderFinished()
    19. }
    20. //将dataId重新赋值为1
    21. dataId = 1
    22. // 如果按下了回车事件
    23. let inputTask = document.querySelector('#task')
    24. inputTask.addEventListener('keydown', function (e) {
    25. if (e.key === 'Enter') {
    26. let newTaskText = inputTask.value.trim()
    27. if (newTaskText !== '') {
    28. arrList1[dataId - 1].push(newTaskText)
    29. localStorage.setItem('arr', JSON.stringify(arrList1))
    30. //更新未完成事件界面
    31. renderUnfinished()
    32. //将input设为空
    33. inputTask.value = ''
    34. }
    35. }
    36. })
    37. // 点击tabs
    38. let tabs = document.querySelector('.tabs')
    39. tabs.addEventListener('click', function (e) {
    40. if (e.target.tagName === 'DIV' && e.target.hasAttribute('data-id')) {
    41. document.querySelector('.tabs .active').classList.remove('active')
    42. e.target.classList.add('active')
    43. // 判断到第几个note
    44. console.log(dataId)
    45. dataId = +e.target.dataset.id
    46. console.log(dataId)
    47. // console.log(dataId)
    48. document.querySelector('.content .active').classList.remove('active')
    49. document.querySelector(`.content .note:nth-child(${dataId})`).classList.add('active')
    50. // 更新两个界面
    51. // renderFinished()
    52. // renderUnfinished()
    53. bindEventListeners()
    54. }
    55. });
    56. // 绑定事件监听器
    57. function bindEventListeners() {
    58. let un_ul = document.querySelector(`.note:nth-child(${dataId}) .uul-unfinished`)
    59. let ul = document.querySelector(`.note:nth-child(${dataId}) .uul-finished`)
    60. let text1 = document.querySelector(`.note:nth-child(${dataId}) .deleteAll`)
    61. // 删除未完成
    62. un_ul.addEventListener('click', function (e) {
    63. if (e.target.classList.contains('delete')) {
    64. let dataID = parseInt(e.target.dataset.id)
    65. arrList1[dataId - 1].splice(dataID, 1)
    66. console.log(arrList1)
    67. localStorage.setItem('arr', JSON.stringify(arrList1))
    68. renderUnfinished()
    69. }
    70. })
    71. // 删除已完成
    72. ul.addEventListener('click', function (e) {
    73. if (e.target.classList.contains('delete')) {
    74. let dataID = parseInt(e.target.dataset.id)
    75. // console.log(dataID)
    76. arrList2[dataId - 1].splice(dataID, 1)
    77. localStorage.setItem('arr1', JSON.stringify(arrList2))
    78. renderFinished()
    79. }
    80. })
    81. //删除全部已完成
    82. text1.addEventListener('click', function (e) {
    83. if (e.target.tagName === 'SPAN') {
    84. arrList2[dataId - 1] = []
    85. localStorage.setItem('arr1', JSON.stringify(arrList2))
    86. renderFinished()
    87. }
    88. })
    89. // 点击未完成按钮任务变成完成
    90. un_ul.addEventListener('click', function (e) {
    91. if (e.target.classList.contains('finished')) {
    92. let dataName = parseInt(e.target.dataset.name);
    93. arrList2[dataId - 1].push(arrList1[dataId - 1][dataName]);
    94. arrList1[dataId - 1].splice(dataName, 1);
    95. localStorage.setItem('arr1', JSON.stringify(arrList2));
    96. localStorage.setItem('arr', JSON.stringify(arrList1));
    97. renderFinished();
    98. renderUnfinished();
    99. }
    100. })
    101. // 时间提醒
    102. un_ul.addEventListener('change', function (e) {
    103. if (e.target && e.target.classList.contains('timeNum')) {
    104. let timer = +e.target.value;
    105. let time = setInterval(function () {
    106. timer--;
    107. let dataName = parseInt(e.target.dataset.name);
    108. if (timer === 0) {
    109. arrList2[dataId - 1].push(arrList1[dataId - 1][dataName]);
    110. arrList1[dataId - 1].splice(dataName, 1);
    111. localStorage.setItem('arr', JSON.stringify(arrList1));
    112. localStorage.setItem('arr1', JSON.stringify(arrList2));
    113. renderFinished();
    114. renderUnfinished();
    115. alert('滴滴,时间到');
    116. clearInterval(time);
    117. }
    118. }, 1000)
    119. }
    120. })
    121. }
    122. // 更新未完成
    123. function renderUnfinished() {
    124. let un_ul = document.querySelector(`.note:nth-child(${dataId}) .uul-unfinished`);
    125. // if (!un_ul) {
    126. // console.error('Element .note:nth-child(${dataId}) .uul-unfinished is not found.');
    127. // return
    128. // }
    129. const listItems = JSON.parse(localStorage.getItem('arr'))[dataId - 1].map((taskText, index) => {
    130. return `
    131. ${taskText}
    132. 定时
    133. ${index}">
    134. `
    135. }).join('')
    136. console.log(JSON.parse(localStorage.getItem('arr'))[dataId - 1])
    137. un_ul.innerHTML = listItems
    138. }
    139. // 更新已完成
    140. function renderFinished() {
    141. let ul = document.querySelector(`.note:nth-child(${dataId}) .uul-finished`)
    142. // if (!ul) {
    143. // console.error('Element .note:nth-child(${dataId}) .uul-finished is not found.')
    144. // return
    145. // }
    146. const listItems = JSON.parse(localStorage.getItem('arr1'))[dataId - 1].map((taskText, index) => {
    147. return `
    148. ${taskText}
    149. 定时
    150. ${index}">
    151. `
    152. }).join('')
    153. ul.innerHTML = listItems
    154. }
    155. // 显示当前时间
    156. function showTime() {
    157. let date = new Date()
    158. // 年月日
    159. let year = date.getFullYear()
    160. let month = date.getMonth() + 1
    161. let day = date.getDate()
    162. // 时分秒
    163. let hour = date.getHours()
    164. hour = hour < 10 ? '0' + hour : hour
    165. let minute = date.getMinutes()
    166. minute = minute < 10 ? '0' + minute : minute
    167. let second = date.getSeconds()
    168. second = second < 10 ? '0' + second : second
    169. // 显示
    170. let element = document.getElementById('date')
    171. element.innerHTML = '

      ' + year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '

      '
    172. }
    173. let ShowTime = window.setInterval(showTime, 1000)
    174. // 初始化事件监听器
    175. bindEventListeners()
    176. })

    到这里就讲完了,感谢大家的观看,希望大家有所收获

  • 相关阅读:
    2023年 DevOps 七大趋势
    Spring 补充@Component 和 注入技巧 问题
    资源共享共赢系统应用
    socket学习二、accept、read、write函数详解
    小程序自定义组件(附“我的“页面item组件代码)
    开发人员必备的万能工具箱:He3
    设计模式之(7)——装饰设计模式
    65 编辑距离
    Hdfs存储策略
    图像细节提取算法
  • 原文地址:https://blog.csdn.net/2301_81253185/article/details/140924135