2009-08-04 1 views
26

A volte è utile avere un oggetto jQuery vuoto, da utilizzare come valore predefinito o come valore iniziale, quando si costruisce un insieme di elementi.jQuery Selettore vuoto (selezione nulla)

Per un esempio, vedere here.

Un modo per farlo sarebbe utilizzare un selettore che è improbabile che corrisponda a qualsiasi cosa, come $('skdhjfksjdhfksjhdf'), ma questo è ovviamente inelegante.

Come posso ottenere un oggetto jQuery vuoto in stile elegante?

+0

Dove si avrebbe bisogno di un selettore vuoto? – Randell

+3

Cosa stai cercando di fare? –

+0

Interessante, qual è il punto? –

risposta

1

Vuoi dire ...

//just get jQuery... 
var foo = $(); 

//or just get the browser using jQuery... 
if($.browser.msie){ 
    alert('You are using the blue e!'); 
} 
+0

o '$ .ajax' o ... – geowa4

31

partire con jQuery 1.4, un semplice $() restituirà un insieme vuoto. jQuery 1.4 release notes ("jQuery() restituisce un set vuoto").

Per le versioni precedenti, utilizzare $([])