JavaScript to add and delete messages on the message board

JavaScript to add and delete messages on the message board

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 results

Run 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');
2. Add elements: node.appendChild(child); // node is the parent element to which the element is added, and child is the created child element. Note: This way of adding elements is similar to the push method in an array, that is, appending elements to add nodes to the specified position: (mainly added to the front of the specified element for display)
node.insertBefore(child, specified element); // "Specified element" refers to the element in front of which child is added. The "specified element" must also be a child element of node

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:
  • JS realizes message board function
  • jsp message board source code three: for jsp beginners.
  • How to use DOM operations to implement a simple message board in js
  • JS+CSS simulates a message board instance that can display content without refreshing
  • JS realizes the message board function [floor effect display]
  • Foldable message board implemented by js (with source code download)
  • jsp message board source code 2: for jsp beginners.
  • My ajax message board source code good application js
  • Code example of writing a message board using ReactJS and Python's Flask framework
  • jsp message board source code 1: for jsp beginners.

<<:  mysql show simple operation example

>>:  Apache Flink arbitrary Jar package upload leads to remote code execution vulnerability recurrence problem (vulnerability warning)

Recommend

Teach you how to achieve vertical centering elegantly (recommended)

Preface There are many ways to center horizontall...

A complete list of common Linux system commands for beginners

Learning Linux commands is the biggest obstacle f...

Tutorial on using iostat command in Linux

Preface It is said that if the people doing opera...

Solution to the docker command exception "permission denied"

In Linux system, newly install docker and enter t...

Introduction to using MySQL commands to create, delete, and query indexes

MySQL database tables can create, view, rebuild a...

Detailed tutorial on running multiple Springboot with Docker

Docker runs multiple Springboot First: Port mappi...

MySQL SQL statement performance tuning simple example

MySQL SQL statement performance tuning simple exa...

How to query or obtain images in a private registry

Docker queries or obtains images in a private reg...

30 minutes to give you a comprehensive understanding of React Hooks

Table of contents Overview 1. useState 1.1 Three ...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

JavaScript to achieve tab switching effect

This article shares the specific code of JavaScri...

A brief analysis of MySQL's lru linked list

1. Briefly describe the traditional LRU linked li...

MySQL compression usage scenarios and solutions

Introduction Describes the use cases and solution...

How to hide rar files in pictures

You can save this logo locally as a .rar file and...