jQuery Document Operation - prepend() Method
Example
Insert content at the beginning of the p element:
$(".btn1").click(function(){ $("p").prepend("<b>Hello world!</b>"); });
Definition and Usage
The prepend() method inserts the specified content at the beginning of the selected element (still located inside).
Tip:prepend() and prependTo() The methods have the same effect. The difference lies in the syntax: the position of content and selector, and prependTo() cannot use functions to insert content.
Syntax
$().prepend(content)
Parameters | Description |
---|---|
content | Required. Specifies the content to be inserted (can include HTML tags). |
Use the function to append content
Use the function to insert the specified content at the beginning of the selected element.
Syntax
$().prepend(function(index, html))
Parameters | Description |
---|---|
function(index, html) |
Required. Specifies the function that returns the content to be inserted.
|