jquery-函数返回;空";-如何检查这个?
我在Jquery Mobile中有一个名为findclosestLink的函数,它检查用户在屏幕上单击时单击的内容 看起来像这样:jquery-函数返回;空";-如何检查这个?,jquery,function,Jquery,Function,我在Jquery Mobile中有一个名为findclosestLink的函数,它检查用户在屏幕上单击时单击的内容 看起来像这样: var link = find(findClosestLink(event.target); findClosestLink: function (ele) { var self = this; while (ele){ if (ele.nodeName.toLowerCase() == "a"){ break;
var link = find(findClosestLink(event.target);
findClosestLink: function (ele) {
var self = this;
while (ele){
if (ele.nodeName.toLowerCase() == "a"){
break;
}
ele = ele.parentNode;
}
console.log(ele);
return ele;
}
如果我单击屏幕ele中的某个位置,控制台显示为“null”,我的变量变为“[]”
问题:我如何检查这个空的东西“[]”
所有这些都不起作用:
if ( link == 'null' ) { console.log("null"); }
if ( link == '' ) { console.log("empty");}
if ( link == 'undefined' ) { console.log("undefined"); }
if ( link == []) { console.log("empty object"); }
if ( link == $() ) { console.log("empty dings"); }
谢谢你的帮助 您的意思是检查空数组吗?使用
length
属性:
console.log([].length); // 0
因此:
你的意思是检查空数组吗?使用
length
属性:
console.log([].length); // 0
因此:
为了进一步解释我的评论:
if ( typeof link == 'null' ) { console.log("null"); }
if ( link == '' ) { console.log("empty");}
if ( typeof link == 'undefined' ) { console.log("undefined"); }
if ( typeof link == 'object') { console.log("empty object"); }
or
switch(typeof link) {
case 'null':
console.log('is null');
break;
[....]
}
为了进一步解释我的评论:
if ( typeof link == 'null' ) { console.log("null"); }
if ( link == '' ) { console.log("empty");}
if ( typeof link == 'undefined' ) { console.log("undefined"); }
if ( typeof link == 'object') { console.log("empty object"); }
or
switch(typeof link) {
case 'null':
console.log('is null');
break;
[....]
}
试试前面的typeof。示例
if(链接类型='null')代码>用前面的typeof试试。示例if(链接类型='null')代码>是的,确实。。。谢谢也是为了改进我的jquery术语词典。一个要移除的“某物”;-)事实上,我认为你的编辑是不正确的。如果数组为空,我不想执行任何操作。正在检查!ele.length仍将激发,因为ele.length=0。如果我检查ele.length!=0,那么它就工作了。是的,的确。。。谢谢也是为了改进我的jquery术语词典。一个要移除的“某物”;-)事实上,我认为你的编辑是不正确的。如果数组为空,我不想执行任何操作。正在检查!ele.length仍将激发,因为ele.length=0。如果我检查ele.length!=0,那么它就可以工作了。下面的代码工作得很好,如果(link.length==0)的话就更容易了。这个函数启动很多次,所以我不想让事情变慢。无论如何谢谢你!下面的代码运行良好,如果(link.length==0),则更容易使用。这个函数启动很多次,所以我不想让事情变慢。无论如何谢谢你!