Possible Duplicate:
$(‘<element>’) vs $(‘<element />’) in jQuery
I am used to write $('<div>')
.
But today I saw a presentation about advanced-jquery by john-resig who use the following syntax $('<div/>')
.
http://loft.bocoup.com/john-resig-advanced-jquery/
To me they seem to produce the same output.
My question is: is there some difference between
$('<div>')
and $('<div/>')
?
No, jQuery will normalize those statements into the exact same.
In some earlier version of jQuery tho, it happend to be that <div>
was actually faster than <div/>
for whatever reason. I don't know yet, if that still applies.
http://jsperf.com/jquery-constructor-performance
Seems like this bug/feature is no longer true.
<div>
, <div/>
, <div></div>
, and even <div/></div>
(Yes, this one will only create one element) all trigger the singleTag
regular rexpression which makes jQuery to call document.createElement("div")
. It was never passed to any html parser at all.
Here's the regex that you can play with, if it returns true, it will be document.createElement'd
var rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
<div>
is an opening tag. <div/>
is a self-closing tag. In this context, however, there is no difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With