The .html() method is not available in XML documents.
When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.
Consider the following HTML:
The content of
.html( "
All new content. You bet!
" );That line of code will replace everything inside
All new content. You bet!
As of jQuery 1.4, the .html() method allows the HTML content to be set by passing in a function. $( "div.demo-container" ).html(function() {
var emphasis = "" + $( "p" ).length + " paragraphs!";
return "
All new content for " + emphasis + "
";});
Given a document with six paragraphs, this example will set the HTML of
All new content for 6 paragraphs!
.This method uses the browser's innerHTML property. Some browsers may not generate a DOM that exactly replicates the HTML source provided. For example, Internet Explorer prior to version 8 will convert all href properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate compatibility layer.
To set the content of a
Note: In Internet Explorer up to and including version 9, setting the text content of an HTML element may corrupt the text nodes of its children that are being removed from the document as a result of the operation. If you are keeping references to these DOM elements and need them to be unchanged, use .empty().html( string ) instead of .html(string) so that the elements are removed from the document before the new string is assigned to the element.