English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
For before() the expression is chosen before the function, with the content as a parameter
The insertBefore() method inserts the specified HTML element before the selected element.
To insert an HTML element after the selected element, useinsertAfter()method.
Inbefore()and the insertBefore() method performs the same task. The main difference lies in the syntax:
Usebefore()The expression is chosen before the function, with the content as a parameter
UseinsertBefore()The content is just opposite, it will be placed in front of the element specified by the parameter
$(content).insertBefore(selector)
Insert HTML elements before each paragraph:
$("button").click(function(){ $("<p style="39;color:red;'>Hello world</p>").insertBefore("p"); });Test and see‹/›
Use the insertBefore() method to insert an existing element before each selected element:
$("button").click(function(){ $("h1").insertBefore("p"); });Test and see‹/›
Parameter | Description |
---|---|
content | Specify the content to be inserted (must contain HTML tags) Possible values:
|
selector | The selected element will be inserted before the element specified by this parameter |