Latest web development tutorials

jQuery insertBefore () method

jQuery HTML / CSS Methods jQuery HTML / CSS Methods

Examples

Insert a <span> element before each <p> element:

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

try it"

Definition and Usage

insertBefore () method before the selected element into an HTML element.

Tip: If you need after being selected element into an HTML element, use insertAfter () method.


grammar

$(content).insertBefore(selector)

参数 描述
content 必需。规定要插入的内容(必须包含 HTML 标签)。

注意:如果content是已存在的元素,它将从它的当前位置被移除,并被插入在被选元素之前。
selector 必需。规定在何处插入内容。


Examples

More examples

Insert an existing element
How to use insertBefore () method before each selected element into an existing element.


jQuery HTML / CSS Methods jQuery HTML / CSS Methods