如何在jQuery中检查'undefined'值 [英] How to check 'undefined' value in jQuery

查看:150
本文介绍了如何在jQuery中检查'undefined'值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在JavaScript中检测未定义的对象属性

javascript未定义比较

我们如何添加对未定义变量的检查,喜欢:

How we can add a check for an undefined variable, like:

function A(val) {
  if (val == undefined) 
    // do this
  else
    // do this
}


推荐答案

JQuery库是专门为简化和统一某些JavaScript功能而开发的。

JQuery library was developed specifically to simplify and to unify certain JavaScript functionality.

但是如果你需要针对<$ c $检查一个变量c> undefined 值,不需要发明任何特殊方法,因为JavaScript有 typeof 运算符,简单,快速且跨平台:

However if you need to check a variable against undefined value, there is no need to invent any special method, since JavaScript has a typeof operator, which is simple, fast and cross-platform:

if (typeof value === "undefined") {
    // ...
}

它返回一个字符串,指示变量或其他未评估操作数的类型。与 if(value === undefined){...} 相比,此方法的主要优点是 typeof value 不存在,c>永远不会引发异常。

It returns a string indicating the type of the variable or other unevaluated operand. The main advantage of this method, compared to if (value === undefined) { ... }, is that typeof will never raise an exception in case if variable value does not exist.

这篇关于如何在jQuery中检查'undefined'值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆