在if条件下将变量与空jquery对象进行比较

时间:2013-06-29 12:19:37

标签: javascript jquery

在jQuery手风琴API中,它说“如果手风琴折叠,ui.newHeader和ui.newPanel将是空的jQuery对象。”

如何检查ui.newheader是否为空的jQuery对象?我试过这样的话:

if ($(ui.newHeader) == null)
{
    ...
}

,像这样:

if (ui.newHeader == null)
{
    ...
}

和此:

if ($(ui.newHeader) == "")
{
    ...
}

基本上,这是一个关于jquery / javascript语法的问题:)谢谢

3 个答案:

答案 0 :(得分:5)

你想知道的是集合中是否有0个元素。这样做:

if ($(ui.newHeader).length==0) {

答案 1 :(得分:1)

if (!$(ui.newHeader).length)

if (!$(ui.newHeader)[0])

答案 2 :(得分:1)

jQuery对象就像集合一样。所以,它是空的,它的length属性是0

if(!$(ui.newHeader).length) {...}