JQuery Getting Started Guide (4): add and delete Elements

Source: Internet
Author: User

Append ()-insert content at the end of the selected element •
Prepend ()-insert content at the beginning of the selected element •

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

$("p").append("Some appended text.");

$("p").prepend("Some prepended text.");

However, the append () and prepend () methods can receive an infinite number of new elements through parameters. You can use jQuery to generate text/HTML (as in the preceding example), or use JavaScript code and DOM elements.
In the following example, we create several new elements. These elements can be created using text/HTML, jQuery, or JavaScript/DOM. Then we use the append () method
Append these new elements to the text (which is equally valid for prepend ):
Instance

Function appendText () {var txt1 = "<p> Text. </p> "; // create a new element var txt2 in HTML = $ (" <p> </p> "). text ("Text. "); // create a new element var txt3 = document with jQuery. createElement ("p"); // create a new element named txt3.innerHTML = "Text. "; $ (" p "). append (txt1, txt2, txt3); // append a new element}

The jQuery before () method inserts content before the selected element.
Instance

$("img").after("Some text after");$("img").before("Some text before");

Function afterText () {var txt1 = "<B> I </B> "; // create a new element in HTML var txt2 =$ ("<I> </I> "). text ("love"); // create a new element var txt3 = document through jQuery. createElement ("big"); // create a new element named txt3.innerHTML = "jQuery! "; $ (" Img "). after (txt1, txt2, txt3); // Insert new elements after img}

$("#div1").remove();
$("#div1").empty();

The jQuery remove () method also accepts a parameter that allows you to filter the deleted elements. This parameter can be the syntax of any jQuery selector.
The following example deletes all <p> elements of class = "italic:
$("p").remove(".italic");


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.