jQuery prependTo()Method

jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Example

Insert a <span> element at the beginning of each <p> element:

$("button").click(function(){ $("<span>Hello World!</span>").prependTo("p"); });

Try it Yourself »

Definition and Usage

The prependTo() method inserts HTML elements at the beginning of the selected elements.

Tip:To insert HTML elements at the end of the selected elements, use theappendTo()method.


Syntax

$(content).prependTo(selector)

Parameter Description
content Required. Specifies the content to insert (must contain HTML tags).

Note:Ifcontentis an existing element, it will be removed from its current position and inserted at the beginning of the selected elements.
selector Required. Specifies which element to prepend the content to.


实例

More Examples

Insert existing elements
How to use the prependTo() method to insert existing elements at the beginning of each selected element.


jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Other Extensions