JavaScript () JQuery HTML join and delete elements

Source: Internet
Author: User

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

four jQuery methods for adding new HTML content:
    • Append ()-inserts content at the end of the selected element
    • Prepend ()-Inserts content at the beginning of the selected element
    • After ()-Inserts the content after the selected element
    • Before ()-insert content before selected element

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.");

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");

So you can see the difference between the first two and the last two.

。 Anyway, I'm going to wonder ... On the Code Analysis:

<! DOCTYPE html>
Feel the stickers good trouble.

。。 Or a descriptive narrative of the word.

。 After completing the above 4 buttons, the effect:

before fantasy after

before

Still fantasy

After

What did you find out?

。。

Continue, review elements:

Append () and prepend () methods to insert content within the selected element (<p> ... ......</p>);
After () and before () methods, outside the selected element (......<p> </p> ) to insert content.




JQuery-Deleting elements
With JQuery, you can delete existing HTML elements very easily.

To delete elements and content, you can typically use the following two JQuery methods:

    • Remove ()-Deletes the selected element (and its child elements)
    • Empty ()-Removes child elements from the selected element

The jQuery Remove () method deletes the selected element and its child elements.
$ ("#div1"). Remove ();

The JQuery Empty () method deletes the child elements of the selected element.


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

Filter for deleted elements
The JQuery Remove () method can also accept a number of parameters, agreeing to filter the deleted element.


The parameter can be the syntax of any jQuery selector.
The following sample removes all <p> elements of the class= "italic":

$ ("P"). Remove (". Italic");
This place I think about, "delete class=" italic "all <p> elements" and "delete <p> all class=" italic "element" is the same.

。。

That

$ (". Italic"). Remove ("P");
so can the empty () method accept a number of parameters? The answer is in the negative.


Another problem. See this section of code:
var txt2=$ ("<p></p>"). Text ("text.");   Create a new element in JQuery
Why can it be written in this form? Not quite sure of the rules.



(20140603 additional understanding of the above questions)

jquery core function: jquery (html,[ownerdocument])
Dynamically creates a DOM element wrapped by a JQuery object, based on the supplied original HTML tag string.

Set a series of properties, events, and so on at the same time.

You can pass a handwritten HTML string, or a string created by some template engine or plug-in. It can also be a string that comes in via AJAX.

However, there are limitations when you create the input element, and you can refer to the second demo sample. Of course, this string can include a slash (for example, an image address) and a backslash. (These are not yet well understood and can look directly at the back.)


When you create a single element. Use either a closed label or XHTML format.

Like what. Create a span that can be used with $ ("<span/>") or $ ("<span></span>") , but not recommended $ ("<span>"). In jquery, this syntax is equivalent to $ (document.createelement ("span")).

The above code is equivalent to:

var txt2=$ ("<p>Text.</p>");   Create a new element in JQuery

Using variables and directly using $ ("<></>") is not the same.

Finally put a section of code as a summary:

<! DOCTYPE html>
Now, it just solves the problem. After a detailed re-study,.

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

JavaScript () JQuery HTML join and delete elements

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.