index.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. * {
  10. padding: 0;
  11. margin: 0;
  12. }
  13. .on {
  14. overflow: none;
  15. white-space: none;
  16. text-overflow: none;
  17. cursor: pointer;
  18. }
  19. button {
  20. cursor: pointer;
  21. }
  22. .todo_list {
  23. min-width: 300px;
  24. max-width: 60%;
  25. height: 800px;
  26. margin: 20px auto;
  27. border-style: dotted solid double dashed;
  28. /* padding: 10px; */
  29. position: relative;
  30. }
  31. .left_cli {
  32. overflow: hidden;
  33. white-space: nowrap;
  34. text-overflow: ellipsis;
  35. cursor: pointer;
  36. }
  37. .dialog {
  38. width: 100%;
  39. height: 100%;
  40. background: rgba(0, 0, 0, .6);
  41. position: fixed;
  42. top: 0;
  43. left: 0;
  44. z-index: 99;
  45. display: none;
  46. }
  47. .top {
  48. height: 100px;
  49. /* outline: red solid 1px; */
  50. }
  51. .title {
  52. text-align: center;
  53. }
  54. .search {
  55. text-align: center;
  56. display: flex;
  57. }
  58. .search input {
  59. margin: 10px;
  60. width: 70%;
  61. height: 30px;
  62. padding-left: 5px;
  63. }
  64. .search button {
  65. flex: 1;
  66. height: 30px;
  67. background-color: #1a1b1c;
  68. border-color: #272728;
  69. color: #fff;
  70. border-radius: 5px;
  71. border: none;
  72. margin: 12px 5px 0 0;
  73. }
  74. .head {
  75. /* height: 50px; */
  76. }
  77. .content {
  78. height: 600px;
  79. /* outline: yellow solid 1px; */
  80. }
  81. .list {
  82. margin-top: 10px;
  83. height: 30px;
  84. display: flex;
  85. text-align: center;
  86. border-bottom: 2px dashed #ddd;
  87. }
  88. .left {
  89. flex: 1;
  90. font-size: 14px;
  91. }
  92. .time {
  93. flex: 1;
  94. font-size: 14px;
  95. text-align: center;
  96. }
  97. .operation {
  98. /* width: 20%; */
  99. /* text-align: center; */
  100. display: flex;
  101. padding-right: 15px;
  102. }
  103. .operation button {
  104. margin-left: 5px;
  105. background-color: #428bca;
  106. border-color: #357ebd;
  107. color: #fff;
  108. border-radius: 5px;
  109. border: none;
  110. flex: 1;
  111. height: 22px;
  112. line-height: 22px;
  113. padding: 0 4px;
  114. }
  115. .listtodo {
  116. /* margin: 12px 0; */
  117. display: flex;
  118. padding: 8px 0px 8px 10px;
  119. border-bottom: 1px dashed #ddd;
  120. }
  121. .line {
  122. text-decoration: line-through;
  123. color: red;
  124. }
  125. .wrap_dialog {
  126. width: 30%;
  127. position: relative;
  128. /* margin: 0 auto 50px; */
  129. background: #fff;
  130. border-radius: 2px;
  131. box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  132. box-sizing: border-box;
  133. padding: 20px;
  134. position: absolute;
  135. top: 50%;
  136. left: 50%;
  137. transform: translate(-50%, -50%);
  138. }
  139. input {
  140. outline: none;
  141. }
  142. .wrap_dialog textarea {
  143. width: 95%;
  144. height: 100px;
  145. border: 1px solid #ccc;
  146. padding: 10px;
  147. resize: none;
  148. border-radius: 5px;
  149. outline: none;
  150. }
  151. .wrap_dialog h2 {
  152. font-size: 16px;
  153. margin-bottom: 15px;
  154. }
  155. .wrap_dialog div {
  156. margin-top: 40px;
  157. text-align: right;
  158. }
  159. .wrap_dialog div button {
  160. height: 30px;
  161. padding: 0 10px;
  162. border: none;
  163. }
  164. .wrap_dialog div button:last-child {
  165. background-color: #428bca;
  166. color: #fff;
  167. margin-right: 10px;
  168. }
  169. .wrap_dialog div button:first-child {
  170. background-color: #fff;
  171. border: 1px solid #ccc;
  172. /* color: #fff; */
  173. padding: 0 10px;
  174. }
  175. #imgObj {
  176. cursor: pointer;
  177. }
  178. </style>
  179. </head>
  180. <body>
  181. <div class="todo_list">
  182. <div class="top">
  183. <div class="title">
  184. <h2 id="updatecontent" onclick="w_getData()">闻达笔记</h2>
  185. </div>
  186. <div class="search">
  187. <input type="text" id="todo" onkeyup="this.value=this.value.replace(/[, ]/g,'')">
  188. <button id="add">添加</button>
  189. <button id="search">查找</button>
  190. </div>
  191. </div>
  192. <div class="head">
  193. <div class="list">
  194. <div class="left">
  195. <h3>事件</h3>
  196. </div>
  197. <div class="time">
  198. <h3>时间</h3>
  199. </div>
  200. <div class="operation">
  201. <h3>操作</h3>
  202. </div>
  203. </div>
  204. </div>
  205. <div class="content">
  206. <!-- <div class="listtodo">
  207. <div class="left">
  208. 123
  209. </div>
  210. <div class="time">
  211. 2019.15.26
  212. </div>
  213. <div class="operation">
  214. <button>done</button>
  215. <button>delete</button>
  216. </div>
  217. </div> -->
  218. </div>
  219. <div class="dialog">
  220. <div class="wrap_dialog">
  221. <h2>编辑</h2>
  222. <textarea></textarea>
  223. <div>
  224. <button id="close" onclick="state(0)">取消</button>
  225. <button id="determine">确定</button>
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. <script src="js/jquery.min.js"></script>
  231. <script>
  232. //id标识便于筛选
  233. var count = 0;
  234. // 第一次加载执行
  235. window.onload = function () {
  236. w_getData();
  237. }
  238. //调取本地存储展示在页面 w_前缀是为了方便智能查找
  239. function w_getData() {
  240. var data = JSON.parse(localStorage.getItem('todoList'));
  241. var arr = [];
  242. //没有直接return掉
  243. if (data == null) {
  244. return
  245. }
  246. $('.content').html('');
  247. for (var i = 0; i < data.length; i++) {
  248. if (data[i].line == true) {
  249. var html = `<div class="listtodo" listid="${data[i].id}"><div class="left line">${data[i].things}</div><div class="time line">${data[i].time}</div><div class="operation"><button id="dones">done</button><button id="delete">delete</button></div></div>`;
  250. } else {
  251. var html = `<div class="listtodo" listid="${data[i].id}"><img id="imgObj" width="12" style="margin: 3px 8px 0 0;opacity: .5;" height="12" src="./edit.png"/>`;
  252. if (data[i].things.length > 15) {
  253. html += `<div class="left left_cli">${data[i].things}</div><div class="time">${data[i].time}</div><div class="operation"><button id="done">done</button><button id="delete">delete</button></div></div>`;
  254. } else {
  255. html += `<div class="left">${data[i].things}</div><div class="time">${data[i].time}</div><div class="operation"><button id="done">done</button><button id="delete">delete</button></div></div>`;
  256. }
  257. }
  258. $('.content').append(html);
  259. if (data[i].id != null) {
  260. arr.push(data[i].id);
  261. }
  262. }
  263. if (arr.length > 0) {
  264. var max = Math.max.apply(null, arr);
  265. count = max + 1;
  266. }
  267. }
  268. $(function () {
  269. //添加事件
  270. $('#add').click(function () {
  271. // 非空验证
  272. if ($('#todo').val() == '') {
  273. return
  274. }
  275. //获取时间
  276. var time = w_nowTime();
  277. //todoList
  278. var todoList = [];
  279. // 先获取下本地是否存有
  280. var historyTodoList = JSON.parse(localStorage.getItem("todoList"));
  281. if (historyTodoList) {
  282. //本地有
  283. var todo = {};
  284. todo.things = $('#todo').val();
  285. todo.time = time;
  286. todo.id = count;
  287. historyTodoList.push(todo);
  288. localStorage.setItem('todoList', JSON.stringify(historyTodoList));
  289. count++;
  290. } else {
  291. //本地無
  292. var todo = {};
  293. todo.things = $('#todo').val();
  294. todo.time = time;
  295. todo.id = count;
  296. todoList.push(todo);
  297. localStorage.setItem('todoList', JSON.stringify(todoList));
  298. count++;
  299. }
  300. //存储完成后清空输入框
  301. $('#todo').val('');
  302. // 显示在任务列表
  303. w_getData();
  304. })
  305. $('#search').click(function () {
  306. var text = $('#todo').val();//获取文本框输入
  307. if ($.trim(text) != "") {
  308. $(".content .listtodo").hide().filter(":contains('" + text + "')").show();
  309. } else {
  310. $(".content .listtodo").show()
  311. }
  312. })
  313. // 已完成直接划掉(采用事件委托的方式,方式新增html元素找不到事件)
  314. $(document).on('click', '#done', function () {
  315. $(this).parent().siblings('time').addClass('line')
  316. $(this).parent().siblings('left').addClass('line')
  317. $(this).parent().siblings('#imgObj').hide()
  318. let listids = $(this).parent().parent().attr('listid');
  319. let datas = JSON.parse(localStorage.getItem('todoList'));
  320. let resIndexs = datas.findIndex(v => {
  321. return v.id === parseInt(listids);
  322. })
  323. datas[resIndexs].line = true;
  324. localStorage.setItem('todoList', JSON.stringify(datas));
  325. w_getData();
  326. })
  327. $(document).on('click', '#dones', function () {
  328. $(this).parent().siblings('.time').removeClass('line')
  329. $(this).parent().siblings('.left').addClass('line')
  330. let listids = $(this).parent().parent().attr('listid');
  331. let datas = JSON.parse(localStorage.getItem('todoList'));
  332. let resIndexs = datas.findIndex(v => {
  333. return v.id === parseInt(listids);
  334. })
  335. datas[resIndexs].line = false;
  336. localStorage.setItem('todoList', JSON.stringify(datas));
  337. w_getData();
  338. })
  339. //delete删除事件(采用事件委托的方式,方式新增html元素找不到事件)
  340. $(document).on('click', '#delete', function () {
  341. let listid = $(this).parent().parent().attr('listid');
  342. let data = JSON.parse(localStorage.getItem('todoList'));
  343. // ES6 提供了两个数组的实例查找相关的方法
  344. // ① Array.find
  345. // ② Array.findIndex
  346. let res = data.find(v => {
  347. return v.id === parseInt(listid);
  348. })
  349. let resIndex = data.findIndex(v => {
  350. return v.id === parseInt(listid);
  351. })
  352. console.log(res, resIndex)
  353. data.splice(resIndex, 1);
  354. //删除后存储
  355. localStorage.setItem('todoList', JSON.stringify(data));
  356. w_getData();
  357. })
  358. //刷新页面
  359. function updatecontent(){
  360. w_getData();
  361. }
  362. //时间函数
  363. function w_nowTime() {
  364. var myDate = new Date();
  365. var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
  366. var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
  367. var day = myDate.getDate(); //获取当前日(1-31)
  368. var week = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
  369. var hour = myDate.getHours(); //获取当前小时数(0-23)
  370. var minutes = myDate.getMinutes(); //获取当前分钟数(0-59)
  371. var seconds = myDate.getSeconds(); //获取当前秒数(0-59)
  372. switch (week) {
  373. case 0:
  374. week = `日`
  375. break;
  376. case 1:
  377. week = `一`
  378. break;
  379. case 2:
  380. week = `二`
  381. break;
  382. case 3:
  383. week = `三`
  384. break;
  385. case 4:
  386. week = `四`
  387. break;
  388. case 5:
  389. week = `五`
  390. break;
  391. case 6:
  392. week = `六`
  393. break;
  394. default:
  395. }
  396. return `${year}年${w_zero(month)}月${w_zero(day)}日${w_zero(hour)}:${w_zero(minutes)}:${w_zero(seconds)}星期${week}`
  397. }
  398. //不够补零
  399. function w_zero(num) {
  400. if (num < 10) return "0" + num; else return num;
  401. }
  402. })
  403. // enter添加事件
  404. $('#todo').keydown(function (event) {
  405. var e = event || window.event;
  406. if (e.keyCode == 13) {
  407. $('#add').click();
  408. }
  409. });
  410. $(document).on('click', '.content .listtodo .left_cli', function () {
  411. $(this).addClass('on').removeClass('left_cli');
  412. })
  413. $(document).on('click', '.content .listtodo .on', function () {
  414. $(this).addClass('left_cli').removeClass('on')
  415. })
  416. var ItemId = ''
  417. $(document).on('click', '#imgObj', function () {
  418. ItemId = $(this).parent().attr('listid');
  419. state(1)
  420. $('.dialog textarea').val($(this).siblings('.left').html())
  421. })
  422. $(document).on('click', '#determine', function () {
  423. state(0)
  424. let data = JSON.parse(localStorage.getItem('todoList'));
  425. for (var i = 0; i < data.length; i++) {
  426. if (data[i].id === parseInt(ItemId)) {
  427. data[i].things = $('.dialog textarea').val()
  428. }
  429. }
  430. let resIndex = data.findIndex(v => {
  431. return v.id === parseInt(ItemId);
  432. })
  433. localStorage.setItem('todoList', JSON.stringify(data));
  434. w_getData();
  435. })
  436. function state(val) {
  437. if (val == 1) {
  438. $('.dialog').show()
  439. } else {
  440. $('.dialog').hide()
  441. }
  442. }
  443. </script>
  444. </body>
  445. </html>
粤ICP备19079148号