Latest web development tutorials

jQuery html () method

jQuery HTML / CSS Methods jQuery HTML / CSS Methods

Examples

Change the contents of all <p> element:

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

try it"

Definition and Usage

html () method sets or returns the selected element contents (innerHTML).

When this method is used to return the contents of the first matching element content is returned.

When this method is used to set the content, rewrite all matching elements content.

Tip: Just set or return the selected elements of the text, use the text () method.


grammar

Back Content:

$(selector).html()

Settings:

$(selector).html( content )

Using the function Settings:

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

参数 描述
content 必需。规定被选元素的新内容(可包含 HTML 标签)。
function(index,currentcontent) 可选。规定返回被选元素的新内容的函数。
  • index- 返回集合中元素的 index 位置。
  • currentcontent- 返回被选元素的当前 HTML 内容。


Examples

More examples

Returns the element content
How to return the contents of the element.

Use the function to set the element content
Use the function to set all the selected elements content.


jQuery HTML / CSS Methods jQuery HTML / CSS Methods