2013-07-01 53 views
1

我正在尝试编写if语句,该语句通过is()检查节点的父节点是否是三个选择器之一,但is()不支持多个选择器作为参数。我怎样才能做到与此类似?:如何使用多个is()选择器?

if ($(this).parent().is('.first','.second','.third')) { 
    //do this thing 
} else { 
    //do this other thing 
} 

回答

4

.first, .second, .third是一个有效的选择:

$(this).parent().is('.first, .second, .third') 
1

退房在API documentation of is()的例子:

你可以指定一个逗号分隔的列表:

if ($(this).parent().is('.first,.second,.third')) { 
    //do this thing 
} else { 
    //do this other thing 
} 
+0

虽然你的例子是正确的,但我不认为它是明确的探究在API文档中进行了修改或举例说明。 – nipponese

+0

@nipponese它是。在else if子句中,$(this).is(“。blue,.red”)' – djf

+0

我纠正了。 – nipponese

相关问题