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
Preface There are many ways to center horizontall...
Learning Linux commands is the biggest obstacle f...
Preface It is said that if the people doing opera...
In Linux system, newly install docker and enter t...
MySQL database tables can create, view, rebuild a...
Docker runs multiple Springboot First: Port mappi...
MySQL SQL statement performance tuning simple exa...
Docker queries or obtains images in a private reg...
Table of contents Overview 1. useState 1.1 Three ...
Inside the style tag of the vue component, there ...
This article shares the specific code of JavaScri...
1. Briefly describe the traditional LRU linked li...
Introduction Describes the use cases and solution...
1. Download MySQL Community Server 5.7.16 and ins...
You can save this logo locally as a .rar file and...