Método jQuery insertBefore()
Ejemplo
Inserte un elemento <span> antes de cada elemento <p>:
$("button").click(function(){
$("<span>Hello world!</span>").insertBefore("p");
});
Definición y uso
El método insertBefore() inserta elementos HTML antes de los elementos seleccionados.
Sugerencia: para insertar elementos HTML después de los elementos seleccionados, utilice el método insertAfter() .
Sintaxis
$(content).insertBefore(selector)
Parameter | Description |
---|---|
content | Required. Specifies the content to insert (must contain HTML tags). Note: If content is an existing element, it will be moved from its current position, and inserted before the selected elements. |
selector | Required. Specifies where to insert the content |
Pruébelo usted mismo - Ejemplos
Cómo usar el método insertBefore() para insertar un elemento existente antes de cada elemento seleccionado.