jQuery html()Methods

jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Examples

Change the content of all <p> elements:

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

Try it Yourself »

Definition and Usage

The html() method sets or returns the content (innerHTML) of the selected elements.

When this method is used toreturncontent, it returns the content of the first matched element.

When this method is used tosetcontent, it overwrites the content of all matched elements.

Tip:If you only need to set or return the text content of the selected elements, use thetext()method.


Syntax

Return content:

$(selector).html()

Set content:

$(selector).html(content)

Set content using a function:

$(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- The index position of the element in the set.
  • currentcontent- The current HTML content of the selected element.


实例

More Examples

Return element content
How to return the content of an element.

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


jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Other Extensions