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
<br /> This article is translated from allwe...
Click here to return to the 123WORDPRESS.COM HTML ...
Table of contents 3 ways to deploy projects with ...
This article uses an example to describe how MySQ...
0x00 Introduction A few months ago, I found a vul...
text-shadow Add a shadow to the text. You can add...
1. Download the successfully generated icon file, ...
There are two types of dead link formats defined b...
This error is often encountered by novices. This ...
This article example shares the specific code of ...
register The front-end uses axios in vue to pass ...
Preface In some cases, we only know the intranet ...
Problem Description MySQL is started successfully...
Copy code The code is as follows: <body <fo...
First, there is only one change event. changeleve...