Phương thức jQuery insertBefore ()

❮ Phương thức HTML / CSS jQuery

Thí dụ

Chèn phần tử <span> trước mỗi phần tử <p>:

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

Định nghĩa và Cách sử dụng

Phương thức insertBefore () chèn các phần tử HTML vào trước các phần tử đã chọn.

Mẹo: Để chèn các phần tử HTML sau các phần tử đã chọn, hãy sử dụng phương thức insertAfter () .


Cú pháp

$(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

Hãy tự mình thử - Ví dụ


Cách sử dụng phương thức insertBefore () để chèn một phần tử hiện có vào trước mỗi phần tử được chọn.


❮ Phương thức HTML / CSS jQuery