jQuery implements a simple comment area

jQuery implements a simple comment area

This article shares the specific code of jQuery to implement a simple comment area for your reference. The specific content is as follows

1. Let’s take a look at the effect first

①Click "Publish"

②Click "Delete Comment"

2. How to implement

First, we use HTML and CSS to write such an area:

HTML content:

<div id="box">
 <div id="fabu">
 <textarea placeholder="Please enter the content!!!" id="text"></textarea>
 </div>
 
 <button onclick="fun1()" id="btn_1">Publish</button>
 
 <div id="pinlun"> </div>
</div>

① Let's first write a big box to wrap all the contents inside

②Write a text, a publish button, and a comment area

③Click the button and the onclick event will execute the function fun1()

CSS content:

*{ 
 padding: 0;
 margin: 0;
 }
 #box{
 width: 600px;
 background-color: aqua;
 margin: 0 auto;
 }
 #fabu{
 width: 600px;
 height: 300px;
 background-color: burlywood;
 }
 #pinlu{
 width: 600px;
 background-color: aqua;
 }
 textarea{
 width: 600px;
 height:300px;
 border: none;
 background-color: burlywood;
 font-size: 24px;
 }
 #btn_1{
 width: 600px;
 height: 30px;
 background-color: cornflowerblue;
 outline: none;
 }
 ::placeholder{
 font-size: 24px;
 }
 #btn_2{
 width: 80px;
 height: 30px;
 background-color: brown;
 border-radius: 4px;
 
 }
 
 p{text-align: right;}
 #neirong{
 background-color: coral;
 border: 1px solid burlywood;
 
}

① *{} Let's first set the default inner and outer margins of all elements to 0

②Then set the corresponding styles for each element accordingly

③Use the ::placeholder pseudo-element tag to set the size of the prompt text

④ We do not set the height of the parent box and the div of the comment area, and let it expand according to the amount of comment content.

function fun1(){
 $('#pinlun').append(
  "<div id='neirong'>" + text.value+"<br><p><button id='btn_2'>Delete comment</button></p></div>");
  text.value="";}

 (function fun2() {
  $("#pinlun").on("click", "button", function() {
  $(this).parent().parent().remove();
 })})()

①jQuery uses $("selector") to get elements

②The append() method adds content (including tags) to the specified element

③When we click to execute fun1(), we also need to set the content to empty ( text.value="")

④Because the pixels that appear when you click are added by the browser later

1. So we cannot find the element if we bind the listener event directly (it will report that the element is undefined)
2. So we use jQuery to set an immediate execution function fun2()
3. The on method in fun2(): element a.on("listen event", "element in element a", try function), so you don't have to worry about whether the element is undefined

⑤Because the function is bound to the button, this is the button. To delete, we must delete the content of the div, and parent() is to find the parent element.

⑥The father of button's father is the added div, remove() method: delete the element

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:
  • jQuery Sina Netease comment block production
  • jQuery implements the scrolling effect of Sina Weibo comments
  • Implementing Ajax non-refresh comments based on jQuery
  • Simple non-refresh comment function example implemented by jQuery
  • jQuery implements comment rating, good and bad review effects
  • JQuery implements the method of dynamically adding and deleting comments
  • PHP combined with jQuery to achieve the comment up and down function
  • jQuery implements star review code based on ajax
  • C# uses jQuery to implement non-refresh comment submission method
  • Beautiful star rating review component code based on jQuery

<<:  Realization of real-time file synchronization between Linux servers

>>:  mysql query data from one table and insert it into another table implementation method

Recommend

Tips for writing concise React components

Table of contents Avoid using the spread operator...

js to realize a simple disc clock

This article shares the specific code of js to im...

Some tips on using the HTML title attribute correctly

If you want to hide content from users of phones, ...

Detailed explanation of how Tomcat implements asynchronous Servlet

Preface Through my previous Tomcat series of arti...

How to quickly delete all tables in MySQL without deleting the database

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

Significantly optimize the size of PNG images with CSS mask (recommended)

This article is welcome to be shared and aggregat...

ffmpeg Chinese parameter description and usage examples

1. When ffmpeg pushes video files, the encoding f...

JavaScript implements password box input verification

Sometimes it is necessary to perform simple verif...

PNG Alpha Transparency in IE6 (Complete Collection)

Many people say that IE6 does not support PNG tra...

How to implement Docker Registry to build a private image warehouse

The image of the microservice will be uploaded to...

How to reset MySQL root password under Windows

Today I found that WordPress could not connect to...

Detailed Explanation of JavaScript Framework Design Patterns

Table of contents mvc mvp mvvm The source of Vue ...

Tutorial on installing mysql under centos7

Recently, I plan to deploy a cloud disk on my hom...

Solution to the horizontal scroll bar in iframe under IE6

The situation is as follows: (PS: The red box repr...

Perfect solution to Google Chrome autofill problem

In Google Chrome, after successful login, Google ...