Donnerstag, 4. März 2010

jQuery: Selecting by ID

<div id="randomid" >

You can make jQuery faster by giving it less information about the element you want to select. In Version 1.4+ jQuery optimizes the selecting logic so that $('#randomid') is super fast. So do not do this:

$('div#randomid') // IDs are unique, so the div is unnecessary
$('div[id="randomid"]') // #id is faster
$('body #randomid') // slower, since we have to find body first
$('#randomid',context) // the context makes jQuery use Sizzle, which is slow!
$('#context #randomid') // see above
$('#context').find('#randomid') // Hell, no!
So stick with the nice and simple:

$('#randomid').do(something)

MfG Hase

Keine Kommentare:

Kommentar veröffentlichen