jquery and JavaScript nodes insert elements in a way that compares _javascript techniques

Source: Internet
Author: User

Second, insert element:

<div>
<p> Facing the sea, spring flowers </p>
</div>

(i), jquery method

1. Insert inside the node:

method

description

Append ()

appendto ()

prepend ()

Specifically implemented as follows:

 $ ("div"). Append ("<p> This is the content </p>" added by the Append ());//Insert paragraph $ ("Div") at the first child node position under the DIV element
 . prepend ("<p> This is the content added by the prepend () method </p>);//insert paragraph at the last child node position under the DIV element

The following two methods are more in line with the general thinking of people, but the effect is the same

 $ ("<p> This is the content added by the Appendto method </p>"). Appendto ("div");//insert paragraph to the first child node position of the DIV element
 $ ("<p> This is the content added by the Prependto method </p> "). Prependto (" div ");//insert paragraph to the last child node of the DIV element

2. Insert outside the node:

Method

Description

After ()

Insert content after each matching element

Before ()

Insert content before each matching element

InsertAfter ()

Inserts all matching elements into the back of another specified set of elements

InsertBefore ()

Inserts all matching elements in front of another specified set of elements

Specifically implemented as follows:

$ ("div"). After ("<p> This is the after () method added content </p>")//Insert paragraph
$ ("div") after the DIV element. Before ("<p> this is before () Method added </p> ");//insert paragraph before DIV element
$ ("div"). After ("<p> This is the after () method added content </p>")//Insert paragraph
$ ("div") after the DIV element. Before ("<p> this is before () Method added </p> ");//insert paragraph before DIV element

3, Appendto (), Prependto (), InsertBefore (), InsertAfter () method has destructive operational characteristics, that is, if the selection already exists and inserts them into the specified object, the contents of the original position will be deleted. The following example selects and moves the paragraph text contained in the original DIV element to the back of the DIV element. The demo is as follows:
 $("p").insertAfter("div");

(ii), JavaScript method

1. Insert inside the node: appendchild () —--corresponds to jquery append (), insertbefore ()---corresponds to prepend in jquery ()

See the jquery method above for specific effects ...

2, custom JavaScript extended DOM function ================ to be continued ~

The above is the content of this article, I hope to help you, thank you for the support of cloud habitat community!

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.