How jquery replicates DOM nodes _jquery

Source: Internet
Author: User

The example in this article describes the way jquery replicates DOM nodes. Share to everyone for your reference. The specific analysis is as follows:

Replication nodes are also one of the most common DOM operations, such as the effects of many shopping sites, where users can buy products not only by clicking the "Select" button below the product, but also by dragging the item by the mouse and placing it in the cart. This product drag function is to use the replication node, the user selected items in the node element copy once, and follow the mouse to move, so as to achieve the shopping effect.

The HTML DOM structure is as follows:

<p class= "nm_p" title= "Welcome to Cloud Communities" > Welcome to Cloud-Habitat </p>
<ul class= "Nm_ul" >
  <li title= ' PHP programming ' > Easy to understand PHP programming </li>
  <li title= ' C programming ' > Easy to understand C programming </li>
  <li title= ' JavaScript programming ' > Easy to understand JavaScript programming </li>
  <li title= ' jquery ' > Easy to understand jquery programming </li>
</ul>

If you need to copy a <li> element after clicking the <li> element, you can do so using the Clone () method.

The jquery code is as follows:

$ (function () {
  $ (". Nm_ul li"). Click (function () {
    $ (this). Clone (True). Appendto (". Nm_ul");
    Copy the currently clicked node and append it to the <ul> element
  }) 
;

When you click a random item in the page, the new node for the item appears at the bottom of the list.

After a node is replicated, the new element being copied does not have any behavior. If you need a new element that also has replication capabilities (in this case, click events), you can use the following jquery code:

$ ("ul Li"). Click (function () { 
 $ (this). Clone (True). Appendto ("ul");//Note the parameter true 
 //can copy yourself, and his copy has the same function. 
})

A parameter true is passed in the Clone () method, meaning that the event that is bound in the element is replicated while copying the element. Therefore, the copy of the element also has a copy function (in this case, click the event).

I hope this article will help you with your jquery programming.

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.