jQuery html () Metode

Metode HTML/CSS jQuery

Contoh

Ubah konten semua elemen <p>:

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

Definisi dan Penggunaan

Metode html() menyetel atau mengembalikan konten (innerHTML) dari elemen yang dipilih.

Ketika metode ini digunakan untuk mengembalikan konten, metode ini mengembalikan konten elemen yang cocok PERTAMA.

Ketika metode ini digunakan untuk menyetel konten, metode ini akan menimpa konten dari SEMUA elemen yang cocok.

Tip: Untuk menyetel atau mengembalikan hanya konten teks dari elemen yang dipilih, gunakan metode text() .


Sintaksis

Kembalikan konten:

$(selector).html()

Tetapkan konten:

$(selector).html(content)

Setel konten menggunakan fungsi:

$(selector).html(function(index,currentcontent))

Parameter Description
content Required. Specifies the new content for the selected elements (can contain HTML tags)
function(index,currentcontent) Optional. Specifies a function that returns the new content for the selected elements
  • index - Returns the index position of the element in the set
  • currentcontent - Returns the current HTML content of the selected element

Cobalah Sendiri - Contoh


Cara mengembalikan konten elemen.


Menggunakan fungsi untuk mengatur konten semua elemen yang dipilih.


Metode HTML/CSS jQuery