Introduction and Application of jquery element control (append element/Append content)

Source: Internet
Author: User

Introduction and Application of jquery element control (append element/Append content)
1. append the element append inside or outside the element. prepend: add it to the child element.
Before, after: Add as a sibling Element

Html:

Append a news post to me
Js:
<Script type = "text/javascript"> jQuery (function () {// Append content inside the element $ ("# content "). append ("Yao Ming retired... ") ;}) </script>

Add an element to # content.

Yao retired...

Add a child element to # content. If you want to append an element outside the element, you need to use "after ".

Yao retired...

Append as a sibling element to the end of # content.

<Script type = "text/javascript"> jQuery (function () {// Add span to the end of content1 $ ("# content "). after ("Yao retired... ") ;}) </script>


2. Append content to different elements

Html:

The moonlight in front of the window is suspected to be frost on the ground
Js:
<Script type = "text/javascript"> jQuery (function () {// Add span to the end of content1 $ ("span "). appendTo ("# content1") ;}) </script>

3. Add content at the beginning of an element

Html:

Append a news post to me
Js:
<Script type = "text/javascript"> jQuery (function () {// Append content at the beginning of an element $ ("# content "). prepend ("Header") ;}) </script>

4. Add content at the beginning of different elements

Html:

Append a piece of news to my end, which is suspected to be ground cream.
Js:
<Script type = "text/javascript"> jQuery (function () {// Append content at the beginning of different elements $ ("span "). prependTo ("# content") ;}) </script>

Add an element to # content as a sub-element to the beginning of # content. If you want to add a start element outside the element, use before, in this way, we can append the suspected ground cream to # content as a sibling element.

<Script type = "text/javascript"> jQuery (function () {// Append content at the beginning of different elements $ ("span "). before ("# content") ;}) </script>

5. contain elements with elements of the specified structure

Warp (html)
Specify the html element in the specified html, but the specified element cannot contain the sibling element. Otherwise, it cannot run normally and does not contain a common text string, the following code uses the div tag to include the p tag.

Html:

 
Js:
<script type="text/javascript"> jQuery(function(){ $("span").wrap(""); }) </script> 

6. contain multiple html elements with specified elements

WrapAll (html)

Html:

 
Js:
<script type="text/javascript"> jQuery(function(){ $("p").wrapAll("  "); }) </script> 

7. Use a specified tag to include child elements

WrapInner (html)
Like the warp method, html elements are specified in html, for example:
The code for using the B label to contain the text in the p label is as follows:

Html:

Include the text of the p tag with the B tag

The p label text is also included with the B label

Js:
javascript <script type="text/javascript"> jQuery(function(){ $("p").wrapInner(""); }) </script> 



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.