Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between $('<div>') and $('<div/>') [duplicate]

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/>')?

like image 580
Lorraine Bernard Avatar asked Dec 12 '12 17:12

Lorraine Bernard


3 Answers

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.

like image 56
jAndy Avatar answered Oct 18 '22 02:10

jAndy


<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>|)$/;
like image 43
Esailija Avatar answered Oct 18 '22 04:10

Esailija


<div> is an opening tag. <div/> is a self-closing tag. In this context, however, there is no difference.

like image 20
Niet the Dark Absol Avatar answered Oct 18 '22 04:10

Niet the Dark Absol