Use Javascript to implement a message board example (with message deletion) for your reference. The specific content is as follows I am still learning the big front end. Please forgive me if there are any irregularities or incorrect ideas in the code. Thank you for your advice. In the discussion area of some websites, we can usually see the message board function. Then when users comment, empty comments cannot be posted. The latest comments will be displayed, and the old comments will be pushed down. Then the blogger can delete the comments. The code is as follows: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } body { padding: 100px; } textarea { width: 200px; height: 100px; border: 1px solid pink; outline: none; resize: none; } ul { margin-top: 50px; } li { list-style: none; width: 300px; padding: 5px; background-color: rgb(245, 209, 243); color: red; font-size: 14px; margin: 15px 0; } li a { float: right; text-decoration: none; } </style> </head> <body> <textarea name="" id=""></textarea> <button>Publish</button> <ul> </ul> <script> var btn = document.querySelector('button') var textarea = document.querySelector('textarea') var ul = document.querySelector('ul') btn.onclick = function () { if (textarea.value == '') { alert('Ning has no input') return false }else{ var li = document.createElement('li') li.innerHTML=textarea.value+"<a href='javascript:;'>Delete</a>" ul.insertBefore(li,ul.children[0]) var as=document.querySelectorAll('a') for (var i=0;i<as.length;i++){ as[i].onclick=function () { ul.removeChild(this.parentNode) } } } } </script> </body> </html> Effect display When comment is empty: New comments will push old comments down: When deleting: Code Explanation Here, when the button click event is triggered, the content in the text field is obtained, the function is triggered, and a judgment is made first to determine whether the value of the text field is empty. If so, a warning box pops up and the text content is not displayed below. If the text box has content, then create an element li and use li to receive '. Then set the text content in li to the content of 1 in the text area + a tag (delete function). Then set the newly added li to be displayed in the front, which is insertbefore. Then we need to bind the a tag in a loop, so that the row will be deleted when the a tag is clicked. 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:
|
<<: Docker MQTT installation and use tutorial
>>: Introduction to Jenkins and how to deploy Jenkins with Docker
Under the requirements of today's responsive ...
A data volume container is a container specifical...
Why? The simplest way to put it is that pixels are...
This article uses an example to describe how MySQ...
<br />This is an article I collected a long ...
Quickstart Guide The Foreman installer is a colle...
MySQL is the most popular relational database man...
<br />The information on web pages is mainly...
Table of contents Local Mixin Global Mixins Summa...
The image can be easily pushed directly to the Do...
MySQL version 5.0 began to support stored procedu...
Table of contents Preface: 1. Understand lock wai...
I have been depressed for a long time, why? Some t...
Table of contents Preface Install Introduction Sw...
This article shares the installation and configur...