The difference between append, prepend, before and after methods in jquery (i)

Source: Internet
Author: User

Original: http://blog.csdn.net/woosido123/article/details/64439490 in jquery append () and prepend () is to insert content within an element (the content becomes a child element or node of the element), after () and before () are inserted outside the element (the sibling node whose content becomes an element). 1. Append () and prepend ()
<div class=‘a‘>   <div class=‘b‘>b</div></div>
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

Use

$(‘.a‘).append($(‘.c‘));
    • 1
    • 1

The effect is as follows:

<div class=‘a‘> //<---you want div c to append in this  <div class=‘b‘>b</div> <div class=‘c‘>c</div></div>
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

Same use

$(‘.a‘).prepend($(‘.c‘));
    • 1
    • 2
    • 1
    • 2

The effect is as follows:

<div class=‘a‘>   <div class=‘c‘>c</div> <div class=‘b‘>b</div></div>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
2. Use after () and before ()

Also use the Assumption code:

$(‘.a‘).after($(‘.c‘));
    • 1
    • 1

The effect is as follows:

<div class=‘a‘>  <div class=‘b‘>b</div></div><div class=‘c‘>c</div>
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

also use before ()

$(‘.a‘).before($(‘.c‘));
    • 1
    • 1

The effect is as follows:

<div class=‘c‘>c</div><div class=‘a‘> <div class=‘b‘>b</div></div>
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

The difference between append, prepend, before, and after methods in jquery (i)

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.