This article shares a small example of adding and deleting messages on a JavaScript message board, with detailed code analysis, but does not involve database operations, only analyzing the underlying code implementation ideas: Achieve resultsRun code interface: Enter a message to add: (The latest message will be displayed at the top) Delete message: (Click on any message to delete it) The main functions and effects are shown. HTML and CSS styles are not affected here. The JS code is shown below: <script> // Get the required element object var text = document.querySelector('textarea'); var btn = document.querySelector('button'); var ul = document.querySelector('ul'); // Register event btn.onclick = function () { // Bind click event to publish button if (text.value == '') { // Determine whether text.value is empty, that is, whether the user has input content alert('You cannot publish empty content!'); return false; } else { // If the user has input content, the content is obtained and assigned to the created element li for display // 1. Create element var li = document.createElement('li'); li.innerHTML = text.value + "<a href='javascript:;' title='Delete this message'>Delete</a>"; // Assign the content entered by the user to the created li element and add an a tag at the end for subsequent deletion of the message // 2. Add elements // ul.appendChild(li); // This way the message is appended to the later display ul.insertBefore(li, ul.children[0]); // Let the newly added message be displayed at the top, that is, in the order from bottom to top // Delete elements: Delete the li node where the current a tag is located, that is, its parent element var as = document.querySelectorAll('a'); for (var i = 0; i < as.length; i++) { as[i].onclick = function () { // The li element to be deleted is the parent element of a, that is, this.parentNode; ul.removeChild(this.parentNode); // Delete the li node in ul, and li is the parent node of the current a tag, pay attention to the relationship between them} } } text.value = ''; // Finally, clear the content in the message input box to facilitate another message} </script> Core knowledge:Add an element node to the page: We want to add a new element to the page in two steps: 1. Create the element; 2. Add the element 1. Create an element: element.createElement('created element tag'); Delete the page element node: node.removeChild(child); // node is the parent element, child is a child element in node Main implementation idea: Here we mainly use the functions of adding nodes to the page and deleting nodes, and bind the two functions to different buttons. For example, the function of adding nodes is given to the "Publish" button, and the function of deleting nodes is given to the "Delete" button, thus realizing a simple version of the message board case. For detailed analysis, it is recommended to combine JS code with detailed comments. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: mysql show simple operation example
Configuration Example upstream backend { server b...
Table of contents Linux netstat command 1. Detail...
Preface This article mainly introduces the releva...
First, let me explain that what we want to do is ...
The four property values of position are: 1.rel...
By using Nginx virtual domain name configuration,...
This article shares the specific code of js to im...
The most understandable explanation of the accura...
The installation of mysql-5.7.17 is introduced be...
This article example shares the specific code of ...
Stored Functions What is a stored function: It en...
:is dynamic component Use v-bind:is="compone...
Abstract: This article mainly explains how to ins...
I would like to share the installation and config...
I have never been able to figure out whether the ...