jQuery判断变量是否为空的5种方式介绍

299 ℃

在项目开发中,我们经常会遇到需要判断变量是否为空的情况,下面web建站小编给大家简单介绍5种jQuery判断变量是否为空​的代码!

1、利用length属性判断

var arr = [];  
if (arr.length > 0) {
  console.log("变量不为空");
} else {
  console.log("变量为空");
}​​

2、利用if语句判断

var str = "";  
if (str) {
  console.log("变量不为空");
} else {
  console.log("变量为空");
}

3、利用!==判断变量是否为undefined或null

var variable;
if (variable !== undefined && variable !== null) {
  console.log("变量不为空");
} else {
  console.log("变量为空");
}

4、$.trim方法判断字符串是否为空格

var str = "   ";
if ($.trim(str).length > 0) {
  console.log("变量不为空");
} else {
  console.log("变量为空");
}

5、利用jQuery的isEmptyObject方法判断

var obj = {};  
if (!$.isEmptyObject(obj)) {
  console.log("变量不为空");
} else {
  console.log("变量为空");
}

jQuery检测指定链接(URL)是否有效(包含跨域)

jQuery语法判断元素是否有子元素的代码?

jQuery语法判断元素是否具有某个特定属性的方法?

jQuery如何判断对象/变量是否为空

jquery关于空对象isEmptyObject()运用方法

标签: $.trim方法, isEmptyObject, jQuery判断

上面是“jQuery判断变量是否为空的5种方式介绍”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_14614.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

javascript关于数组中使用include方法
NoSQL数据库的特点(NoSQL简介)
如何在自己网站上显示其它网站的权重
sitemap对于SEO优化具有怎样的好处呢
vuejs打包后禁止查看源代码