jQuery insertBefore() method
Example
Insert content after each
Insert an element before the element:
$
(
"
button
"
)
.
click
(
function
(
)
{
$
(
"
Hello world!
"
)
.
insertBefore
(
"
p
"
)
;
}
)
;
Try it »
Definition and Usage
The insertBefore() method inserts HTML elements before the selected elements.
**Tip:** To insert HTML elements after the selected elements, use insertAfter() method.
Syntax
$(
content
).insertBefore(
selector
)
| Parameters | Description |
|---|---|
| content | Required. Specifies the content to insert (must contain HTML tags). |
**注意:**如果 *content* 是已存在的元素,它将从它的当前位置被移除,并被插入在被选元素之前。 |
| selector | Required. Specifies where to insert the content. |
More examples
Inserting an existing element
How to use the insertBefore() method to insert an existing element before each selected element.
jQuery HTML/CSS methods