JavaScript (20) add and delete elements in jQuery HTML

Source: Internet
Author: User

JQuery-add element
With jQuery, you can easily add new elements/content.

Four jQuery methods for adding new HTML content:

  • 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
    The jQuery append () method inserts 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.");

    The jQuery after () method inserts content after the selected element.
    $("img").after("Some text after");

    The jQuery before () method inserts content before the selected element.
    $("img").before("Some text before");

    What is the difference between the first two and the last two... I'm thinking about it... Code Analysis:
    <Script src = "jquery-1.11.1.js"> </script> <script> $ (document ). ready (function () {$ ("# btn1 "). click (function () {$ ("# test1 "). prepend ("Before") ;}); $ (" # Btn2 "). click (function () {$ (" # test1 "). append ("After") ;}); $ (" # Btn3 "). click (function () {$ (" # test2 "). before ("Before") ;}); $ (" # Btn4 "). click (function () {$ (" # test2 "). after ("After") ;}); </Script>Add text in front of Fan tesiAdd text after fan terseyAdd text before fan tersiAdd text after still fan tersi

    Versace

    Still ventsi

    It feels troublesome to map images... Or text description... After clicking the four buttons, the effect is as follows:

    BeforeVersaceAfter

    Before

    Still ventsi

    After

    Did you find something... Continue, review elements:

    Append () and prepend () methods, within the selected element (

    ............

    ) Insert content;
    After () and befZ finished? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> memory + ceiling/SBqUXVlcnmjrL/J0tS63Mjd0te12Mm + s/ceiling + ceiling/ycq508PS1M/ceiling + PGJyPgo8dWw + ceiling s/second + CjxwcmUgY2xhc3M9 "brush: java; "> $ (" # div1 "). remove ();
    The jQuery empty () method deletes the child elements of the selected element.
    $("#div1").empty();

    Filter deleted elements
    The jQuery remove () method can also accept a parameter that allows filtering of deleted elements.
    This parameter can be the syntax of any jQuery selector.
    The following example deletes all

    Element:

    $("p").remove(".italic");
    Here I thought about how to "delete all class =" italic"

    Element and delete

    All class = "italic" elements "... That is:

    $(".italic").remove("p");
    Can the empty () method accept a parameter? The answer is no!


    Another problem is that the following code is displayed:
    Var txt2 = $ ("

    "). Text (" Text. "); // create a new element with jQuery
    Why can it be written in the above form? I am not quite clear about the rules... As you study in the future, you should know that day, haha... Put it here first ......

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.