html5离线记事本

时间:2025-01-21 11:36:56
html5离线记事本
离线记事本

这是一个笔记应用,不需要联网,也不需要数据库,可以直接把数据储存在本地.方便易用! ^_^

  1. <!DOCTYPE html> 

  2. <html> 

  3. <head> 

  4. <meta charset="UTF-8"> 

  5. <title>记事本</title> 

  6. <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> 

  7. <style> 

  8. *{ margin: 0; padding: 0; box-sizing: border-box; font-family: 'Microsoft Yahei'; font-size: 14px; box-sizing: border-box;} 

  9. h1{text-align: center; font-size: 26px; line-height: 60px;color: #ff8140;} 

  10. .main{margin: 10px auto;width: 1000px; overflow: hidden; height: 500px;padding: 1%;border: 1px solid #ddd; border-radius: 5px;} 

  11. input{ width: 92%; padding: 5px; outline: none;border: 1px solid #eee;} 

  12. textarea{ width: 100%; overflow: hidden; padding: 5px; outline: none; resize:none; } 

  13. textarea:focus,input:focus { border-color: #f77c3d; } 

  14. .write{padding: 10px; border-radius: 3px; background: #eee; overflow: hidden; float: left;width: 48%;} 

  15. .send{ background: #ff8140; border: 1px solid #f77c3d; color: #fff; box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.25); 

  16. width: 80px; height: 30px; text-align: center; line-height: 30px; border-radius: 3px; float: right; cursor: pointer; } 

  17. .list{padding: 10px; float: left;width: 50%;} 

  18. .item{padding: 10px;border: 1px solid #eee;border-radius: 3px;margin-bottom: 10px;} 

  19. .item .content{padding: 20px 10px;word-break:break-all;background: #fff; display: none;} 

  20. .title{padding-bottom: 5px;border-bottom: 1px solid #eee;cursor: pointer;} 

  21. .title span{color: #999; font-size: 12px;float: right;} 

  22. #noteList{overflow-y: scroll;height: 350px;box-shadow: 0 0 3px #ddd;} 

  23. </style> 

  24. </head> 

  25. <body> 

  26. <h1>记事本</h1> 

  27. <div class="main"> 

  28. <div class="write" id="write"> 

  29. 标题: <input id="title" type="text" placeholder="请输入标题"><br><br> 

  30. 分类: <select id="type"> 

  31. <option value="默认" selected>-请选择-</option> 

  32. <option value="美食">美食</option> 

  33. <option value="代码">代码</option> 

  34. <option value="生活">生活</option> 

  35. </select> 

  36. <br><br> 

  37. <textarea name="" id="cont" cols="30" rows="10" placeholder="今天想说点啥..."></textarea> 

  38. <div class="send" id="add">添加</div> 

  39. </div> 

  40. <div class="list" id="list"> 

  41. 标题: <input id="title1" type="text" placeholder="查询标题"><br><br> 

  42. 分类: <select id="type1"> 

  43. <option value="默认" selected>-请选择-</option> 

  44. <option value="美食">美食</option> 

  45. <option value="代码">代码</option> 

  46. <option value="生活">生活</option> 

  47. </select> 

  48. <div id="search" class="send">查询</div><br><br> 

  49. <div id="noteList"></div> 

  50. </div> 

  51. </div> 

  52. <script type="template" id="temp"> 

  53. <div class="item"> 

  54. <div class="title">@title<span>@type: @date</span></div> 

  55. <div class="content">@cont</div> 

  56. </div> 

  57. </script> 

  58. <script> 

  59. $(function(){ 

  60. var init = {title:'这是标题', 

  61. date:new Date().toLocaleString(), 

  62. type:'示例', 

  63. cont:'这是一个笔记应用,不需要联网,也不需要数据库,可以直接把数据储存在本地.方便易用!^_^'}; 

  64. function show(notes){ 

  65. var temp = $('#temp').html(); 

  66. var tempStr= ''; 

  67. //如果不是数组,变成数组 

  68. if(!Array.isArray(notes)){ 

  69. notes = [notes]; 



  70. for(var i=notes.length-1;i>-1;i--){ 

  71. var note = notes[i]; 

  72. tempStr += temp.replace('@title',note.title) 

  73. .replace('@date',note.date) 

  74. .replace('@type',note.type) 

  75. .replace('@cont',note.cont); 



  76. $('#noteList').html(tempStr); 




  77. //读取所有数据 

  78. function showList(){ 

  79. var notes = localStorage.getItem('notes'); 

  80. if(!notes){ 

  81. show(init); 

  82. }else{ 

  83. notes = JSON.parse(notes); 

  84. show(notes); 



  85. //第一个展开 

  86. $('#noteList .item:first').find('.title').trigger('click'); 




  87. $('#add').click(function(){ 

  88. var title = $('#title').val(); 

  89. var cont = $('#cont').val(); 

  90. var type = $('#type').val(); 

  91. if(title == ''){ 

  92. alert('标题不能为空!'); 

  93. return; 



  94. var data = {title:title,cont:cont,type:type,date:new Date().toLocaleString()}; 

  95. var notes = localStorage.getItem('notes'); 

  96. if(!notes){ 

  97. notes = []; 

  98. }else{ 

  99. notes = JSON.parse(notes); 



  100. notes.push(data); 

  101. localStorage.setItem('notes',JSON.stringify(notes)); 

  102. showList(); 

  103. }); 


  104. $('#search').click(function(){ 

  105. var title = $('#title1').val(); 

  106. var type = $('#type1').val(); 

  107. var notes = localStorage.getItem('notes'); 

  108. if(!notes){ 

  109. alert('没有本地笔记数据!'); 

  110. return; 

  111. }else{ 

  112. notes = JSON.parse(notes); 



  113. var note = []; 

  114. for(var i=0;i<notes.length;i++){ 

  115. //notes[i] json对象 

  116. var flag = false; 

  117. if(!title){ 

  118. flag = notes[i].type==type; 

  119. }else if(!type){ 

  120. flag = notes[i].title.indexOf(title)>-1; 

  121. }else{ 

  122. flag = notes[i].title.indexOf(title)>-1 && notes[i].type==type; 



  123. if(flag){ 

  124. note.push(notes[i]); 





  125. if(note.length == 0){ 

  126. alert('抱歉~没有对应的笔记!'); 

  127. }else{ 

  128. show(note); 



  129. }); 


  130. $('body').on('click','#noteList .title', function(){ 

  131. $(this).next().slideToggle(); 

  132. }); 


  133. showList(); 

  134. }) 

  135. </script> 

  136. </body> 

  137. </html>