The difference between append appendTo prepend prependto insertbefore InsertAfter after before in jquery

Source: Internet
Author: User

There is a key element in jquery that operates the same way, but renders different results.

<!DOCTYPE HTML><HTMLLang= "en">  <Head>    <MetaCharSet= "Utf-8">    <title>Test</title>    <Scriptsrc= "Jquery.js"></Script>  </Head>  <style>P{Background-color:Aquamarine;  }  </style>  <Body>    <PID= "N1">      <spanID= "N2">Span#n2</span>N1</P>  <PID= "N3">      <labelID= "N4"class= "Move">Label#n4</label>N3</P>  <PID= "N5">      <spanID= "N6">Span#n6</span>N5</P>  <PID= "N7">    <spanID= "N8">Span#n8</span>N7</P>  <PID= "N9">    <spanID= "N10">Span#n10</span>N9</P>  <PID= "N11">    <spanID= "N12">Span#n12</span>N11</P>  </Body></HTML>

Create a new jquery div object first:

Let Newdiv = $ (' <div>new one</div> ');

Use the following statements, respectively, to see what works.

$ (' #n1 '). Append (Newdiv.clone ()); Newdiv.clone (). InsertAfter (' #n1 '); Newdiv.clone (). AppendTo (' # N3 '); $ (' #n5 '). Prepend (Newdiv.clone ()); $ (' #n5 '). InsertBefore (Newdiv.clone ()); Newdiv.clone (). Prependto (' #n7 ') $ (' #n9 '). Before (Newdiv.clone ()); Newdiv.clone (). Before (' #n9 '); $ ( ' #n11 '). After (Newdiv.clone ()), Newdiv.clone (). After (' #n11 ');

Believe that after the above code you can know some relationships:

1 to and without to is an opposite relationship

2 **pend is added at the end of the P element and becomes P's last-child insert** is added outside the P element, becomes the next of P, or first-sibling

3 A.before (b) = B.insertbefore (a) a.after (b) = B.insertafter (a) The effect is equal, but the return value is not the same, if the use of before returns a, if insert returns a B

Hope that the above summary can be helpful to your understanding.

The difference between append appendTo prepend prependto insertbefore InsertAfter after before in jquery

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.