Phương thức jQuery prependTo ()

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

Thí dụ

Chèn phần tử <span> vào đầu mỗi phần tử <p>:

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

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

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

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


Cú pháp

$(content).prependTo(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 at the beginning of the selected elements.
selector Required. Specifies on which elements to prepend the content to

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


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


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