2013-07-01 56 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

相關問題