Deep understanding of DOM copy clone () and deep dom copy clone

Source: Internet
Author: User

Deep understanding of DOM copy clone () and deep dom copy clone

A clone node is a common DOM operation. jQuery provides a clone method to process dom cloning:

The. clone () method deeply copies all matching element sets, including all matching elements, the lower-level elements of matching elements, and the text nodes.

The clone method is simple to clone a node, but you must note that if the node has other processing tasks such as events or data, we need to pass a Boolean value ture Through clone (ture) to specify, in this way, we not only clone the node structure, but also clone the events and data.

For example:

HTML <div> </div> JavaScript section $ ("div "). on ('click', function () {// execute Operation}) // clone to process 1 $ ("div "). clone () // only cloned structure, event loss // clone process 2 $ ("div "). clone (true) // structure, events, and data are cloned

The usage is as simple as this. We need to know the details when cloning:

  • When using the clone () method, before inserting it into a document, we can modify the cloned element or element content, such as the code on the right side: this(.clone().css ('color', 'red ') added a color.
  • Pass true to copy all event handlers bound to the original element to the cloned element.
  • The clone () method is extended by jQuery and can only process events and data bound by jQuery.
  • Objects and arrays in element data will not be copied, And the cloned elements and original elements will continue to be shared. For all data that is deeply copied, You need to manually copy each

Case Analysis:

<! DOCTYPE html> 

 

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.