Does jQuery simply append the htmlString as a normal string and let the browser do the work, or is there something I'm missing?
Yes, and yes. :-)
jQuery lets the browser do the work, but it does some things to help with situations that might otherwise be problematic. For instance:
var row = $("<tr><td>foo</td></tr>");
You can do that in jQuery, and you get back a jQuery object wrapping a row that isn't (yet) in the DOM, which has a cell in it.
Which seems simple enough, but jQuery has to do work to ensure that that HTML string is parsed within the context of a table (because if you try to make the browser parse it within, say, a div
, there's a problem).
Edit: Rob W pointed to some relevant source for this:
// We have to close these tags to support XHTML (#13200)
wrapMap = {
// Support: IE 9
option: [ 1, "<select multiple='multiple'>", "</select>" ],
thead: [ 1, "<table>", "</table>" ],
col: [ 2, "<table><colgroup>", "</colgroup></table>" ],
tr: [ 2, "<table><tbody>", "</tbody></table>" ],
td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
_default: [ 0, "", "" ]
};
Code in jQuery's internal buildFragment
function uses the map above (and objects derived from it) to ensure the HTML is parsed correctly.