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

Tools to convert static websites into RSS

<br /> This article is translated from allwe...

Markup Language - Print Style Sheets

Click here to return to the 123WORDPRESS.COM HTML ...

Implementation of tomcat deployment project and integration with IDEA

Table of contents 3 ways to deploy projects with ...

Example of how to generate random numbers and concatenate strings in MySQL

This article uses an example to describe how MySQ...

Stealing data using CSS in Firefox

0x00 Introduction A few months ago, I found a vul...

Implementation of CSS text shadow gradually blurring effect

text-shadow Add a shadow to the text. You can add...

favico.ico---Website ico icon setting steps

1. Download the successfully generated icon file, ...

How to create Baidu dead link file

There are two types of dead link formats defined b...

Vue implements paging function

This article example shares the specific code of ...

Django+vue registration and login sample code

register The front-end uses axios in vue to pass ...

How to use ssh tunnel to connect to mysql server

Preface In some cases, we only know the intranet ...

Solution to MySQL startup successfully but not listening to the port

Problem Description MySQL is started successfully...

How to set focus on HTML elements

Copy code The code is as follows: <body <fo...

Vue project @change multiple parameters to pass multiple events

First, there is only one change event. changeleve...