类型转换一个jQuery对象返回未定义。

时间:2022-12-07 20:19:27

I have a question about jQuery. I was wondering if it's possible to typeconvert a jQuery-object to undefined.

我有一个关于jQuery的问题。我在想,是否有可能将一个jquery对象定义为未定义的。

Like this:

是这样的:

alert(typeof $mobileMenu); // Return undefined
$mobileMenu = $('<ul id="mobile-menu"></ul>'); 
alert(typeof $mobileMenu);  // Return object
$mobileMenu = null;  // Here I want it to convert back to undefined.
alert(typeof $mobileMenu);  // This returns "object" but what I want is it to return "undefined" 

Thanks.

谢谢。

2 个解决方案

#1


2  

$mobileMenu = null;

change to

改变

$mobileMenu = undefined;

#2


0  

if want that than you need to do this

如果你想要的话,你需要这样做。

$mobileMenu= undefined; 

null - It generally makes sense and behaves similarly to other scripting languages' concepts of the out-of-band ‘null’, ‘nil’ or ‘None’ objects.

null——它通常是有意义的,它的行为与其他脚本语言的“空”、“nil”或“None”对象的概念类似。

so setting null doesnt make any sense if you want undefined.

所以如果你想没有定义,那么设置null就没有意义了。

Note: The strict equality operator rather than the standard equality operator must be used here, because x == undefined also checks whether x is null, while strict equality doesn't. null is not equivalent to undefined.

注意:这里必须使用严格的等式操作符,而不是标准的相等运算符,因为x ==未定义的值也会检查x是否为空,而严格的相等则不会。null不等于未定义。

#1


2  

$mobileMenu = null;

change to

改变

$mobileMenu = undefined;

#2


0  

if want that than you need to do this

如果你想要的话,你需要这样做。

$mobileMenu= undefined; 

null - It generally makes sense and behaves similarly to other scripting languages' concepts of the out-of-band ‘null’, ‘nil’ or ‘None’ objects.

null——它通常是有意义的,它的行为与其他脚本语言的“空”、“nil”或“None”对象的概念类似。

so setting null doesnt make any sense if you want undefined.

所以如果你想没有定义,那么设置null就没有意义了。

Note: The strict equality operator rather than the standard equality operator must be used here, because x == undefined also checks whether x is null, while strict equality doesn't. null is not equivalent to undefined.

注意:这里必须使用严格的等式操作符,而不是标准的相等运算符,因为x ==未定义的值也会检查x是否为空,而严格的相等则不会。null不等于未定义。