jQuery sebelum () Metode

Metode HTML/CSS jQuery

Contoh

Sisipkan konten sebelum setiap elemen <p>:

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

Definisi dan Penggunaan

Metode before() menyisipkan konten tertentu di depan (sebelum) elemen yang dipilih.

Tip:   Untuk menyisipkan konten setelah elemen yang dipilih, gunakan metode after() .


Sintaksis

$(selector).before(content,function(index))

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

Possible values:

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

Cobalah Sendiri - Contoh


Sisipkan konten dengan metode before().


Cara menggunakan fungsi untuk menyisipkan konten sebelum elemen yang dipilih.


Metode HTML/CSS jQuery