English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The prepend() method inserts the specified content at the beginning of each selected element (as the first child element).
To insert content at the end of the selected element, useappend()Method.
Insert prefix content:
$.selector.prepend(content)
Using a function to add content:
$.selector.prepend(function(index, html))
Add some text content before all paragraphs:
$("button").click(function(){ $("p").prepend("Hello world"); });Test to see‹/›
Add some HTML before all paragraphs:
$("button").click(function(){ $("p").prepend("<b>Hello world</b> });Test to see‹/›
This example uses document.createTextNode() to create a text node and add it before all <p> elements:
$("button").click(function(){ $("p").prepend(document.createTextNode("Hello world")); });Test to see‹/›
Using a function to add content:
$("button").click(function(){ $("p").prepend(function(i){ return "<b>This p element has index " + i + "</b>"; }); });Test to see‹/›
Parameters | Description |
---|---|
content | Specify the content to be inserted at the beginning of each selected element (can include HTML tags) Possible values:
|
function(index, html) | Specify a function that returns the content to be inserted
|