I won't speak to the jQuery documentation, nor jQuery's internal
implementation for $(html), only your questions about HTML and XHTML, based
on my own knowledge and best practices:

*Non-void elements (sometimes called non-empty elements, meaning they can
contain text and child nodes)*
For an empty span element on its own, neither

<span/>

nor

<span>

is technically valid HTML or XHTML, unless you rely on the html parser to
auto-close the element. And the rules for doing that depend on the element,
what follows it, and which html parser is being used (ie, which browser). As
a best practice, I, and the jQuery UI project by convention, always use

<span></span>

as a valid and consistent way to open and close a non-void but initially
empty element. Because many elements have an optional closing tag in html,
according to how browser html parsers behave, many people continue to use

<span>

or

<span/>

and they may never have any problems in any browsers with that, though the
latter is merely an XML-compatible version of the former, not technically
valid XHTML as span is a non-void element. In the case of that (X)HTML being
parsed as html, which is most common, the browser will ignore the slash
inside the opening tag (as if you had specified <span>) and when it self
closes, it will be as if you had specified

<span></span>

*Void elements (sometimes called empty elements, meaning they cannot contain
text or child nodes)*
Void elements never have a closing tag, and don't require a closing slash in
the tag in HTML, but allow it for compatability with XHTML, which does
require it. See

http://wiki.whatwg.org/wiki/FAQ#Should_I_close_empty_elements_with_.2F.3E_or_.3E.3F

This is a relatively short list of elements (this one from
http://www.elharo.com/blog/software-development/web-development/2007/01/29/all-empty-tags-in-html/#comment-227448
):

area
base
basefont
br
col
frame
hr
img
input
isindex
link
meta
param

the most common ones being img and input. Note it does not include some very
commonly created elements such as

a
div
li
ol
span
table
td
th
tr
ul

- Richard

On Thu, Sep 24, 2009 at 11:30 AM, Bertilo Wennergren <berti...@gmail.com>wrote:

>
> According to the jQuery documentation we're
> supposed to write like this:
>
>  $("<span/>")
>
> and not like this:
>
>  $("<span>")
>
> http://docs.jquery.com/Core/jQuery#htmlownerDocument
>
> That means using XHTML style code for the
> elements created.
>
> Are we supposed to do that even when we are using
> HTML style code (e.g. HTML 4.01)?
>
> E.g.:
>
>  $('#whatever').html('<img alt="" src="..." />');
>
> That would seem a bit bizarre if the actual HTML
> page has lots of '<img alt="" src="">' without the
> ending slash.
>
> It does seem that "img", "br" etc. work without the
> ending slash as well.
>
> --
> Bertilo Wennergren <http://bertilow.com>
>

Reply via email to