2012-05-23 12 views
8

Voglio creare una funzione che può essere utilizzata con Id o passando l'oggetto jQuery.Come rilevare l'argomento oggetto jQuery

var $myVar = $('#myId'); 

myFunc($myVar); 
myFunc('myId'); 

function myFunc(value) 
{ 
    // check if value is jQuery or string 
} 

Come è possibile rilevare il tipo di argomento passato alla funzione?

Nota!This domanda non è la stessa. Non voglio passare una stringa di selezione come #id.myClass. Voglio passare l'oggetto jQuery come nell'esempio.

+4

Un oggetto jQuery non è un "selettore jQuery"; è un oggetto. Un ** selettore ** è una stringa con sintassi del selettore CSS. – Pointy

+0

@Pointy Punto buono, modificato – Tx3

+0

Se si desidera rilevare un oggetto jQuery, quindi [la parte di quel post che dice 'valore instanceof di jQuery'] (http://stackoverflow.com/a/6283431/575527) (le mans: rilevare un'istanza di oggetto jQuery) sarebbe la risposta. – Joseph

risposta

15

Utilizzare l'operatore typeof

if (typeof value === 'string') { 
    // it's a string 
} else { 
    // it's something else 
} 

o per fare davvero sicuro che sia un'istanza dell'oggetto jQuery

if (typeof value === 'string') { 
    // it's a string 
} else if (value instanceof $) { 
    // it's a jQuery object 
} else { 
    // something unwanted 
} 
+1

Almeno test rapidi dimostrano che 'value instanceof $' funziona senza intoppi. Questa era la cosa che stavo cercando. Grazie! – Tx3

0

Non sarebbe sufficiente per verificare il tipo dell'argomento?

function myfunc(arg) 
{ 
    if(typeof arg == 'string') 
    { 

    } 
    else if(typeof arg == 'object') 
    { 

    } 
} 

Controllare questo Fiddle.

+0

Questo non considera null, che ha anche typeof = "object". – Marc

0
function myFunc(value) 
{ 
    if (typeof value == "string") { 
     //it's a string 
    } 
    else if (value != null && typeof value == "object"} { 
     //it's an object (presumably jQuery object) 
    } 
    else { 
     //it's null or something else 
    } 


} 
+0

'typeof value' non sarà mai' null'. Intendevi 'valore! = Null'? –

+0

@JamesAllardice, sì, questo è quello che intendevo. Aggiustato. Grazie. Valore – Marc

0

Prova questo:

function myFunc(value) 
{ 
    if(typeof value === 'object') { 
    } 
    else{ 
    } 
} 
0
function myFunc(value) 
{ 
    if(typeof(value) == 'string') 
    //this is a string 
    else if (value === jQuery) 
    //this is jQuery 
    else if (typeof(value) == 'object') 
    //this is an object 
} 

NOTA: fatto questo nella console:

> jQuery 
function (a,b){return new e.fn.init(a,b,h)} 
> var value = jQuery 
undefined 
> value 
function (a,b){return new e.fn.init(a,b,h)} 
> value === jQuery 
true 
+0

=== jQuery fornisce false per l'oggetto jQuery – Tx3

+1

'valore === jQuery' sarà vero solo per jQuery stesso, non per le sue istanze. Vuoi l'operatore 'instanceof'. @ RGB - Si noti che 'typeof' è un operatore, non una funzione, quindi non è necessaria la parentesi (non causerà alcun problema, ma può essere più chiaro senza di essi). –

+0

uhh, sì lo fa ..: P ma vedo che hai una soluzione :) – RGB

3

Ogni oggetto jQuery ha una proprietà jquery. Questo non riuscirà, naturalmente, se il vostro oggetto ha jquery proprietà ... tuttavia è possibile controllare più severe se vuoi ...

function(o) { 
    if(typeof o == 'object' && o.jquery) // it's jquery object. 
} 
0

Provare a utilizzare typeof, ad esempio:

var $myVar = $('#myId'); 

myFunc($myVar); 
myFunc('myId'); 

function myFunc(value){ 
    // check if value is jQuery or string 
    switch(typeof value) { 
     case 'object': 
     // is object 
     break; 

     case 'string': 
     // is string 
     break; 

     // etc etc. 
    } 
}