Four jQuery methods for adding new content: Append,prepend,after,before

Source: Internet
Author: User

The four JQuery methods for adding new content are the following:

Append ()-inserts content at the end of the selected element ( inside )
Prepend ()-Inserts content at the beginning of the selected element ( inside )

// The jQuery Append () method inserts the content at the end of the selected element. $ ("P"). Append ("Some appended text." ); // The jQuery prepend () method inserts content at the beginning of the selected element. $ ("P"). Prepend ("Some prepended text.");
Adding several new elements through the Append () and prepend () methods
function AppendText () {var txt1= "<p>Text.</p>";               // Create new elements in HTML var txt2=$ ("<p></p>"). Text ("text.");   // Create a new element in JQuery var txt3=document.createelement ("P");  // Create a new element in DOM txt3.innerhtml= "Text." ; $ ("P"). Append (TXT1,TXT2,TXT3);         // Append new Element }

After ()-Inserts content after the selected element ( outside )
Before ()-insert content before the selected element ( outside )

$ ("img"). After ("Some text after"); $ ("IMG"). Before ("Some text before");
Adding several new elements through the After () and before () methods
function Aftertext () {var txt1= "<b>i </b>";                    // Create new elements in HTML var txt2=$ ("<i></i>"). Text ("Love");     // Create new elements with JQuery var txt3=document.createelement ("big");  // Create a new element from the DOM txt3.innerhtml= "jquery!" ; $ ("IMG"). After (TXT1,TXT2,TXT3);          // Insert new element after IMG }

Four jQuery methods for adding new content: Append,prepend,after,before

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.