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:<p>Hello</p> how are <p>you?</p>
$("p").remove();
how are
<p>Hello</p> how are <p>you?</p>
$("p").detach();
how are
<p class="hello">Hello</p> how are <p>you?</p>
$("p").detach(".hello");
how are <p>you?</p>
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!