在项目开发中,我们经常会遇到需要判断变量是否为空的情况,下面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关于空对象isEmptyObject()运用方法
标签: $.trim方法, isEmptyObject, jQuery判断
上面是“jQuery判断变量是否为空的5种方式介绍”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_14614.html
workflows工作流
哈利波特魔法ComfyUI工作流
一只被水晶包围的小动物ComfyUI工作流
梦幻向日葵ComfyUI工作流
一个戴着破旧莎草帽的机械忍者
晚上樱花狐狸ComfyUI工作流
一位穿着长袍的强大法师ComfyUI工作流
王家卫电视剧繁花海报效果comfyui工作流
一条色彩斑斓的超现实小孔雀鱼ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!