Home  >  Article  >  Web Front-end  >  The difference between remove() and detach() in jquery

The difference between remove() and detach() in jquery

巴扎黑
巴扎黑Original
2017-06-25 09:53:531421browse

 jquery is 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

Remove all 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:
  e388a4556c0f65e1904146cc1a846beeHello94b3e26ee717c64999d7867364b1b4a3 how are e388a4556c0f65e1904146cc1a846beeyou?94b3e26ee717c64999d7867364b1b4a3
jQuery code:
  $("p").remove();
Result:
  how are
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:
 e388a4556c0f65e1904146cc1a846beeHello94b3e26ee717c64999d7867364b1b4a3 how are e388a4556c0f65e1904146cc1a846beeyou?94b3e26ee717c64999d7867364b1b4a3
jQuery code:
  $("p").detach();
Result:
  how are
Description:

Delete the paragraph with hello class from the DOM

HTML code:
  889c467f53be0c0d4e7d0d985361b565Hello94b3e26ee717c64999d7867364b1b4a3 how are e388a4556c0f65e1904146cc1a846beeyou?94b3e26ee717c64999d7867364b1b4a3
jQuery code:
  $("p").detach(".hello");
Result:
  how are e388a4556c0f65e1904146cc1a846beeyou?94b3e26ee717c64999d7867364b1b4a3
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

controlformValidator for verification. Everyone has used this control. It starts verification when the page is loaded, and for css The display and 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);
                
            }
        }

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 The difference between remove() and detach() in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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