2011-12-15 43 views
1

我在Jquery Mobile中有一个名为findclosestLink的函数,它检查用户在屏幕上单击时点击的内容。jquery - 函数返回“null” - 如何检查这个?

是这样的:

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控制台为 “空” 和我的变量变成 “[]”。

问题:我该如何检查这个空的东西“[]”?

所有这些不工作:

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"); } 

感谢您的帮助!

+0

在前面的typeof尝试。例子`if(typeof link =='null')...' – 2011-12-15 12:52:28

+0

`link == null`怎么样? – 2011-12-15 12:58:01

+0

@Tricker - 不起作用 – frequent 2011-12-15 13:01:08

回答

1

你的意思是检查空数组吗?使用length属性:

console.log([].length); // 0 

这样:

// 0 is 'falsy', so... 
if(!ele.length) { 
    // empty 
} 
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; 
    [....] 
}