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

Source: Internet
Author: User

The Remove () and detach () methods all remove all elements from the DOM

What's common is that none of the matching elements are removed from the jquery object.

The difference is that the element removed with remove (), except that the element is preserved, the other binding events on the element are removed, but the detach () method does not remove the method on the element or append data.

Simple example:

Html:

<body><p> difference </p></body>

Js:

The binding event of the P element $ ("P"). Click (function () {alert ("Remove vs Detach")});

var a=$ ("P"). Remove () A is a jquery object that removes the P element when it re-plugs the retained P element back into the body of $ ("body"). HTML (a[0]), and when you click on the P element, the Click event is not triggered. That is, the event has been removed by the remove event.

var b=$ ("P"). Detach () A is a jquery object that deletes the P element when it re-plugs the retained P element back into the body of $ ("body"). HTML (a[0]), which triggers the Click event when the P element is clicked again. That is, the event remains on the element, and the detach event does not remove the P-element event.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.