Common insertion methods and differences between DMO nodes and dmo nodes

Source: Internet
Author: User

Common insertion methods and differences between DMO nodes and dmo nodes
1. Insert append () and appendTo () in the DOM ()

Dynamic Creation of elements is not enough. It is only temporarily stored in the memory, and finally we need to put it into the page document and present it. So the question is, how to put it on the document?

A positional relationship is involved here. It is common to place the newly created element inside it as a child element of a page element. For such processing, jQuery defines two operation methods.

 

Selector Description
Append ()

Append content or subnodes to each Matching Element

AppendTo ()

Append all matched elements to another specified Element Set.

 

Append: this operation is similar to executing the native appendChild method on the specified elements and adding them to the document.

AppendTo: in fact, the general $ (A) is reversed using this method ). append (B) operation, that is, not to append B to A, but to append A to B.

<! DOCTYPE html> 

Summary:

The. append () and. appendTo () methods have the same functions. The main difference is the syntax-the content and the target location are different.

Before append () is the object to be inserted, followed by the element content to be inserted in the object before appendTo () is the element content to be inserted, followed by the inserted object

 

2. Insert prepend () and prependTo () in the DOM ()

In addition to inserting the specified content at the end (still inside) of the selected element through append and appendTo, operations performed inside the element can also be performed before the selected element, the methods provided by jQuery are prepend and prependTo.

Selector Description
Prepend ()

Insert content at the beginning of the selected Element

PrependTo ()

Place all matched elements in the specified Element Set.

 Tip: The prepend () is reversed ()

 

Usage and differences between prepend and prependTo:

The. prepend () method inserts the specified element into the matching element as its first child element (if you want to insert it as the last child element, use. append ()).
. Prepend () and. prependTo () implement the same function. The main difference is the syntax. The inserted content and the target location are different.
For. prepend (), the selector expression is written before the method and serves as the container for the content to be inserted. The content to be inserted is used as the method parameter.
PrependTo () is the opposite. The content to be inserted is written before the method. It can be a selector expression or dynamically created tag. the container of the content to be inserted is used as a parameter.

<! DOCTYPE html> 

 

Here we will summarize the differences between the four internal operations:
Append () adds content prepend () to each matching element to appendTo () append all matched elements to the set of another specified element, prependTo (), and forward all matched elements to the set of another specified element.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.