Phương thức jQuery prepend ()

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

Thí dụ

Chèn nội dung vào đầu tất cả các phần tử <p>:

$("button").click(function(){
  $("p").prepend("<b>Prepended text</b>");
});

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

Phương thức prepend () chèn nội dung đã chỉ định vào đầu các phần tử đã chọn.

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


Cú pháp

$(selector).prepend(content,function(index,html))

Parameter Description
content Required. Specifies the content to insert (can contain HTML tags)

Possible values:

  • HTML elements
  • jQuery objects
  • DOM elements
function(index,html) Optional. Specifies a function that returns the content to insert
  • index - Returns the index position of the element in the set
  • html - Returns the current HTML of the selected element

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


Chèn nội dung bằng phương thức prepend ().


Sử dụng một hàm để chèn nội dung vào đầu các phần tử đã chọn.


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