2024年05月05日 建站教程
在项目开发中,我们经常会遇到需要判断变量是否为空的情况,下面web建站小编给大家简单介绍5种jQuery判断变量是否为空的代码!
var arr = [];
if (arr.length > 0) {
console.log("变量不为空");
} else {
console.log("变量为空");
}
var str = "";
if (str) {
console.log("变量不为空");
} else {
console.log("变量为空");
}
var variable;
if (variable !== undefined && variable !== null) {
console.log("变量不为空");
} else {
console.log("变量为空");
}
var str = " ";
if ($.trim(str).length > 0) {
console.log("变量不为空");
} else {
console.log("变量为空");
}
var obj = {};
if (!$.isEmptyObject(obj)) {
console.log("变量不为空");
} else {
console.log("变量为空");
}
本文链接:http://so.lmcjl.com/news/3796/