How to copy a DOM node using JQuery, jquerydom Node

Source: Internet
Author: User

How to copy a DOM node using JQuery, jquerydom Node

This document describes how to copy a DOM node using JQuery. Share it with you for your reference. The specific analysis is as follows:

Copying a node is also one of the common DOM operations. For example, you can click the "select" button at the bottom of a product to purchase a product for many shopping websites, you can also drag the item and place it in the shopping cart. This product drag function is to use the copy node to copy the node element of the selected product once, and move it with the mouse to achieve the shopping effect.

The html dom structure is as follows:

<P class = "nm_p" title = "Welcome to "> welcome to </p> <ul class = "nm_ul"> <li title = 'php programming'> simple and easy-to-understand PHP programming </li> <li title = 'C programming '> simple and easy-to-understand C Programming </li> <li title = 'javascript programming'> simple and easy-to-understand javaScript programming </li> <li title = 'jquery '> simple and easy-to-understand JQuery programming </li> </ul>

If you need to copy another <li> element after clicking the <li> element, you can use the clone () method.

The JQuery code is as follows:

$ (Function () {$ (". nm_ul li "). click (function () {$ (this ). clone (true ). appendTo (". nm_ul "); // copy the node currently clicked and append it to <ul> element })});

After you click any item on the page, the new node of the item appears at the bottom of the list.

After a node is copied, the new elements do not have any behavior. If you want to copy a new element (in this example, click an event), you can use the following JQuery code:

$ ("Ul li "). click (function () {$ (this ). clone (true ). appendTo ("ul"); // note that the parameter is true // you can copy yourself, and its copy has the same function. })

In the clone () method, a parameter true is passed, which means that the events bound to the element are copied while copying the element. Therefore, the copy of this element also has the copy function (in this example, click event ).

I hope this article will help you with jQuery programming.

Related Article

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.