Home > Article > Web Front-end > The difference between remove() and detach() in jquery
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
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
$("p").remove();
how are
e388a4556c0f65e1904146cc1a846beeHello94b3e26ee717c64999d7867364b1b4a3 how are e388a4556c0f65e1904146cc1a846beeyou?94b3e26ee717c64999d7867364b1b4a3
$("p").detach();
how are
889c467f53be0c0d4e7d0d985361b565Hello94b3e26ee717c64999d7867364b1b4a3 how are e388a4556c0f65e1904146cc1a846beeyou?94b3e26ee717c64999d7867364b1b4a3
$("p").detach(".hello");
how are e388a4556c0f65e1904146cc1a846beeyou?94b3e26ee717c64999d7867364b1b4a3
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!