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
This article example shares the specific code of ...
Linux change hostname command 1. If you only need...
1. A static page means that there are only HTML ta...
Application Scenario Taking the background manage...
Let me share with you a creative opening realized...
Scenario Description In a certain system, the fun...
Table of contents 1. MySQL trigger creation: 1. M...
When we do CSS web page layout, we all know that i...
List of HTML tags mark type Name or meaning effec...
Generally speaking, after the container is starte...
Preface Slow query log is a very important functi...
1. Install Zabbix Agent to monitor the local mach...
This article shares the specific code of the firs...
Table of contents 1. Get request: 2. Post request...
mysqlslap Common parameter description –auto-gene...