jquery move and copy DOM node implementation program

Source: Internet
Author: User

When you do a project, you need the DOM node to move, as in the following code:

The code is as follows Copy Code

<div></div>

<p></p>

You need to move the P tag to the DIV tag, and after testing, it's convenient to move the DOM node in jquery:

The code is as follows Copy Code

$ (' div '). Append ($ (' P '))

So you can move the P tag to the DIV tag, and don't write this:

The code is as follows Copy Code

$ (' div '). Append ($ (' P '). HTML ())

This simply copies the contents of the P tag into the div tag.

If you just copy one to the div tag, the original label remains, so you can write:

The code is as follows Copy Code

$ (' div '). Append ($ (' P '). Clone (True))

Cases

The code is as follows Copy Code

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

Also, when the clone parameter is set to True, you can copy the event bound by the button to the new button


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, copies of this element also have replication capabilities. If you do not want the event to be replicated, you can write this:

The code is as follows Copy Code

$ (' div '). Append ($ (' P '). Clone ())

Moving nodes
Moving one node on a page to another can be jq the internal and external insertion method (Append,appendto,prepend,prependto,after,before,insertafter,insertbefore), You can move the selected node directly by passing it in

  code is as follows copy code

<button >move me!</button>
<div id= "box" ></DIV>

Instance

$ ("button"). Click (function () {
 $ (This). Appendto ($ ("#box"));
 //or append
 $ ("#box"). Append (this);
};

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.