Analyze the difference between remove() and detach() functions in jquery

巴扎黑
Release: 2017-06-25 09:48:12
Original
1458 people have browsed it

jqueryis a very powerful thing that can often be used in work, but some methods are still ignored by us because they are not commonly used or have not been noticed.

Remove() and detach() may be one of them. Maybe we use remove() more often, but detach() may be less used. Maybe I didn't use it carefully enough. I haven't used it once. But this time I used it because of a problem in a project. I found it very interesting, so I recorded it and shared it with everyone.

Remove(): The official explanation is

Removeall matching elements from the DOM. This method does not remove the matching elements from the jQuery object, so the matching elements can be reused in the future. But in addition to the element itself being retained, other elements such as bound events, additional data, etc. will be removed.

My understanding is that the element is removed. But how to find it again? To be honest, I have never found any friend who has used it. Can you tell me, thank you very much,

Usage:

Delete all paragraphs from DOM

HTML code:
  

Hello

how are

you?

Copy after login
jQuery code:
  $("p").remove();
Copy after login
Result:
  how are
Copy after login
Copy after login
I won’t go into details about this method. Let’s mainly talk about the detach() method,

Official description:

Delete from DOM All matching elements. This method does not remove the matching elements from the jQuery object, so the matching elements can be reused in the future. Unlike remove(), all bound events, attached data, etc. will be retained.

Description:

Remove all paragraphs from DOM

HTML code:
 

Hello

how are

you?

Copy after login
jQuery code:
  $("p").detach();
Copy after login
Result:
  how are
Copy after login
Copy after login
Description:

Delete the paragraph with hello class from the DOM

HTML code:
  

Hello

how are

you?

Copy after login
jQuery code:
  $("p").detach(".hello");
Copy after login
Result:
  how are 

you?

Copy after login
When we look at it this way, there seems to be no difference. These two functions, haha. . . . Let me tell you now what happened to me. Then everyone will understand how to solve it.

I have a form here, one of which is the registration code, that is, each information will have an independent registration code. Without the registration code, the registration cannot be successful. I use jquery's

controlformValidatorfor verification. Everyone has used this control. It starts verification when the page is loaded, and for css Thedisplayand the hide() method in jquery are ignored. Originally. This is no problem, but the user has put forward a new demand, which is to add an option to determine whether to display the registration code. If it is not displayed, then do not verify the registration code text box. This is a shameless demand.

After trying out css display and jquery hide(), I set my sights on remove(). It stopped verifying, but when I chose to verify, the removed content couldn't be added back, so I started looking for something that could be added back. At this time,

detach() was discovered. What's the benefit of it. I will put a code below

var p; function selectChange() { if (document.getElementById("ddl_schoolarea").value != "请选择") { p = $("#trlession").detach(); } else { //table1为一个table名字 $("#table1").append(p); } }
Copy after login

After seeing this code, I don’t think I need to explain too much. Everyone will understand, it’s a very interesting method.


The above is the detailed content of Analyze the difference between remove() and detach() functions in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!