Compare the After (), append (), AppendTo () methods in jquery

Source: Internet
Author: User

HTML page:

<p id= "MYP1" > My hobbies are:</p>
<button id= "B1" >after function </button>
<button id= "B2" >append function </button>
<button id= "B3" >appendto function </button>

JS Page

$ (document). Ready (function () {
$ ("#b1"). Click (function () {
$ ("#myp1"). After ("<b> cycling </b>");
})
$ ("#b2"). Click (function () {
$ ("#myp1"). Append ("<e> play </e>");
})
$ ("#b3"). Click (function () {
$ ("<i> bake </i>"). AppendTo ("#myp1");
})
})

Final Result:

Description, after () method, the inserted element is behind the selection element, and is outside. The result of append (), AppendTo () is the same, except that it is written differently, and the Append () method also inserts the element after the selected element, but inserts it inside the selected element.

In CSS, there is an after pseudo-element to insert a new element into the selected element.

CSS page:

#myp1:: after{
Content: "Cycling," "playing," "baking";
}

Final Result:

As you can see, the CSS pseudo-element is inserted inside the selected element. To achieve the same functionality, the pseudo-element method of CSS seems to be simpler than the append () method in jquery.

Since there is a backward insertion element, there is also a way to insert elements forward, which is not elaborated in detail, the principle is actually the same.

Compare the After (), append (), AppendTo () methods in jquery

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.