Node insertion--contrast jquery and JavaScript methods (i)

Source: Internet
Author: User
Tags javascript extension

Second, insert the element:
<div>2     <p> Facing the Sea, spring </P></div> 

(i) , JQuery Method

1. Insert inside the node:

Method

Description

Append ()

Append content to each matching element

AppendTo ()

Appending all the elements to another specified set of elements actually reverses the use of the Append (). such as $ (a). Append (b) and $ (b). Appendto (a) is equivalent

Prepend ()

Internal front content to each matched element

Prependto ()

Puts all matching elements in front of another specified set of elements. Actually reversed the use of Preprend (). such as $ (A). Prepend (b) and $ (b). Prenpendto (b) equivalent

The specific implementation is as follows:

1 $ ("div"). Append ("<p> This is what the append () method adds </p>");  Insert paragraph 3 $ ("div") in the first sub-node position under the DIV element . prepend ("<p> This is what the prepend () method adds </p>");  inserts a paragraph at the last child node position under a DIV element    

The following two methods are more consistent with people's general thinking, but the effect is the same

1 $ ("<p> This is the content added by the AppendTo method </p>"). AppendTo ("div"); // insert paragraph into the DIV element's first child node position 3 $ ("<p> This is the content added by the Prependto method </p>"). Prependto ("div");  inserts a paragraph into the last child node position of a 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 behind another specified set of elements

InsertBefore ()

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

The specific implementation is as follows:

1 $ ("div"). After ("<p> This is what the after () method adds </p>"); // Insert paragraph 3 $ ("div") after the DIV element. Before ("<p> This is what the before () method adds </p>");  insert a paragraph before a DIV element    
1 $ ("<p> This is the content added by the InsertAfter method </p>"). InsertAfter ("div"); // Insert the paragraph behind the DIV element 3 $ ("<p> This is the InsertBefore method added within </p>"). InsertBefore ("div");  insert a paragraph in front of a DIV element    

3, AppendTo (), Prependto (), InsertBefore (), InsertAfter () methods have destructive operational characteristics, that is, if you select already existing content and insert them into the specified object, the contents of the original location will be deleted. The following example selects and moves the paragraph text contained in the original DIV element behind the div element. The demo is as follows:

1 $ ("P"). InsertAfter ("div");

(ii) , JavaScript Method

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

See the jquery method above for specific effects ...

2. Custom JavaScript extension DOM function ================ to----to be continued

Node insertion--contrast jquery and JavaScript methods (i)

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.