学习用JavaScript对数据进行CRUD操作
一、什么是 CRUD
CRUD
就是我们常说的对数据的 增,查,改,删
操作
- C: Create
- R: Read
- U: Update
- D: Delete
二、 CRUD
程序的基本逻辑
三、留言板小程序-01
页面内有效,关闭或刷新页面后数据会消失
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>留言板</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"> </head> <body> <div> <h1>留言板</h1> <label for="postInput">添加一个留言 </label> <input type="text" id="postInput" value="" /> <button id="insertBtn" onclick="insertPost()">提交</button> <span id="msgSpan" style="color:red;"></span> </div> <hr> <div> <ul id="postUl"> </ul> </div> </body> <script type="text/javascript"> let postInput = document.getElementById("postInput") let postUl = document.getElementById("postUl") let msgSpan = document.getElementById("msgSpan") let postId = 0;
let insertPost = () => { if (postInput.value === "") { msgSpan.innerText = "留言不能为空" return false } postId++ postUl.innerHTML += ` <li id=${postId} >${postInput.value} <i class="fa-solid fa-edit" title="修改" onclick=editPost(${postId})></i> <i class="fa-solid fa-trash-can" title="删除" onclick=delPost(${postId})></i> </li> ` postInput.value = "" msgSpan.innerText = "" }
let editPost = (id) => { let postLi = document.getElementById(id) postInput.value = postLi.innerText.trim() postLi.remove() }
let delPost = (id) => { let postLi = document.getElementById(id) postLi.remove() }
</script> </html>
|
四、留言板小程序-02
将数据保存在浏览器数据库(LocalStorage
)中,关闭或刷新页面后数据不会消失
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>留言板(localstorage)</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"> </head> <body> <div> <h1>留言板(localstorage)</h1> <label for="postInput">添加一个留言 </label> <input type="text" id="postInput" value="" /> <button id="insertBtn" onclick="insertPost()">提交</button> <span id="msgSpan" style="color:red;"></span> </div> <hr> <div> <ul id="postUl"> </ul> </div> </body> <script type="text/javascript"> let postInput = document.getElementById("postInput") let postUl = document.getElementById("postUl") let msgSpan = document.getElementById("msgSpan") let postArray = []
let insertPost = () => { if (postInput.value === "") { msgSpan.innerText = "留言不能为空" return false }
let postId = Math.random().toString(36).substring(7)
postArray.push({ "post_id": postId, "post_val": postInput.value })
localStorage.setItem("posts", JSON.stringify(postArray))
postInput.value = "" msgSpan.innerText = ""
showPosts() }
let editPost = (id) => { postArray.forEach((post) => { if (post.post_id === id) { postInput.value = post.post_val } }) delPost(id) }
let delPost = (id) => { postArray = postArray.filter((post) => { return post.post_id != id }) localStorage.setItem("posts", JSON.stringify(postArray)) showPosts() }
let showPosts = () => { postUl.innerHTML = "" let posts = localStorage.getItem("posts") if (posts != null) { postArray = JSON.parse(posts) postArray.forEach((post) => { postUl.innerHTML += ` <li>${post.post_val} <i class="fa-solid fa-edit" title="修改" onclick=editPost("${post.post_id}")></i> <i class="fa-solid fa-trash-can" title="删除" onclick=delPost("${post.post_id}")></i> </li> ` }) } } showPosts() </script> </html>
|
浏览器中的显示结果