<img> is a tag which doesn't require an ending tag.  <input> is
another as is <br>.  The W3C validator will actually warn you if you
end tags like that, though it's not a big deal.

When jQuery creates the HTML, I believe the code is injected into an
empty element and jQuery sees what the browser made of the string.
The browser decides the proper syntax for the element; your code style
including <'s, etc are ignored (though needed to understand what kind
of input you're giving the $ function).

If you would like a deeper understanding of why these tags have
different endings, look the issue up on google.  It's an interesting
story of a holdover from the early days of HTML (that's why it's found
on these basic, older tags).  And as always, if you're interested a
deeper understanding of jQuery, you might just want to give the $
function's code a look.  It's really fairly simple to follow if you
know your inputs.



On Jan 14, 10:39 pm, RWF <mgor...@gmail.com> wrote:
> This also confused me. I tried creating the element as the1.3 docs
> show (as noted in the OP) and then appending it to a div and it did
> not generate a closing tag. This is what I did:
> var image=$("<img/>");
> image.alt="hello world";
>
> $("#maindiv").append(image);
>
> The result was a single <img> without a / like this:
> <div id=maindiv><img></div>
>
> Am I misunderstanding something?
>
> On Jan 14, 3:13 pm, Ricardo Tomasi <ricardob...@gmail.com> wrote:
>
> > With the string, jQuery has to parse it, to find the tagname and any
> > attributes. For a dozen elements it doesn't make any real difference,
> > but when you get to the hundreds or thousands of elements it's
> > significant:
>
> > 100 elements:
> > String: 34ms
> > DOMElement: 5ms
>
> > 1000 elements:
> > String: 339ms
> > DOMElement: 40ms
>
> > This in Firefox 3, Core 2 Duo 1.6ghz, using Firebug (code below).
>
> > cheers,
> > - ricardo
>
> > (function(){
> > var i=1001, r=i;
> > console.time('string');
> > while(--i){
> >  $('<div/>');}
>
> > console.timeEnd('string');
>
> > i=r;
> > console.time('DOMEl');
> > while(--i){
> >   $(document.createElement('div'));};
>
> > console.timeEnd('DOMEl');
>
> > })();
>
> > On Jan 14, 3:04 pm, thomasvsundert <thomas.vansund...@gmail.com>
> > wrote:
>
> > > Hi,
> > > the new documentation for jQuery 1.3 states that:
>
> > > "To create a span use $("<span/>"). As of jQuery 1.3 this syntax is
> > > completely equivalent to $(document.createElement("span"))."
>
> > > First of all, what does this mean? What was the difference before?
>
> > > My real question is, what is the performance penalty for using $("<div/
>
> > > >") vs $(document.createElement("div"))
>
> > > thanks,
> > > Thomas

Reply via email to