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

Source: Internet
Author: User

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

Assume

<div class=‘a‘> //<---you want div c to append in this  <div class=‘b‘>b</div></div>

Use

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

This will be the case:

<div class=‘a‘> //<---you want div c to append in this  <div class=‘b‘>b</div>  <div class=‘c‘>c</div></div>

Use

$(‘.a‘).prepend($(‘.c‘));

The result is this:

<div class=‘a‘> //<---you want div c to append in this  <div class=‘c‘>c</div>  <div class=‘b‘>b</div></div>
After () and before ()

The same code above, using the

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

Results:

<div class=‘a‘>  <div class=‘b‘>b</div></div><div class=‘c‘>c</div> //<----this will be placed here

Use

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

Results:

<div class=‘c‘>c</div> //<----this will be placed here<div class=‘a‘>  <div class=‘b‘>b</div></div>

From the above we can draw the conclusion that:

Append () & Prepend () inserts content inside the element (the content becomes a child element or node of the element), after () & before () is the insertion of content outside the element (the sibling node whose content becomes an element).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between append () and prepend (), after () and 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.