Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add an element to jquery

Shulou Source: shulou.com Published: 2022-05-31 18:42:46 09月23日 Update

This article mainly introduces jquery how to add an element of related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will gain after reading this jquery article on how to add an element, let's take a look at it.

Add method: 1, use the "$(" specified element "). After (new element)" statement, you can add a sibling element after the specified element; 2, use the "$(" specify element ") .before (new element)" statement, you can add the sibling element in front of the specified element; 3, use the "$(parent element). Append (child element)" statement.

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, you can use the following methods to add elements:

After (): adds a sibling element to the specified element

Before (): adds sibling elements to the specified element

Append (): adds child elements to the end of the specified element

Prepend (): adds a child element to the beginning of the specified element

1. Use after ()

The after () method inserts content "behind" outside the selected element.

$(function () {$("button") .click (function () {var a = "a span element"; $("div") .after (a);}) body * {background-color:pink } A big title

A p paragraph

A div element

A p paragraph

Insert a sibling node after div

2. Use before ()

Before () can insert a sibling node before the specified element

$(function () {$("button") .click (function () {var a = "a span element"; $("div") .before (a);}) body * {background-color:pink } A big title

A p paragraph

A div element

A p paragraph

Insert a sibling node before div

3. Use append ()

The append () method inserts content into the "end" inside the selected element.

$(function () {$("button") .click (function () {var a = "a p element)

"; $(" div ") .append (a);}) body * {background-color:pink;} a big title

A p paragraph

A div element

A p paragraph

Insert a child element at the end of the div

4. Use prepend ()

The prepend () method inserts content into the "start" inside the selected element.

$(function () {$("button") .click (function () {var a = "a p element)

"; $(" div ") .prepend (a);}) body * {background-color:pink;} a big title

A p paragraph

A div element

A p paragraph

Insert a child element at the beginning of the div

This is the end of the article on "how to add an element to jquery". Thank you for reading! I believe you all have a certain understanding of "how to add an element to jquery". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Element paragraph sibling content method title end knowledge node statement size article value brother easy to operate tutorial article easy to understand more version Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Docker MariaDB OPPO Reno