2010-03-31 86 views

回答

228

使用:not selector

$(".thisclass:not(#thisid)").doAction(); 

如果有多個ID或選擇只使用逗號分隔符,除了:在選擇

(".thisclass:not(#thisid,#thatid)").doAction();

+2

如果您想要排除多個類別,該怎麼辦? THX – SoulMagnet 2014-02-03 13:41:05

+12

從文檔:'所有選擇器被接受裏面:not(),例如::not(div a)和:not(div,a)'所以只需使用逗號分隔的選擇器來執行多個'(「。這個類:不是(#thisid,#thatid)「)。doAction();' – Chausser 2014-03-11 01:22:45

+0

使用單引號這樣 - $(」。thisclass:not('#thisid')「)。doAction(); – 2015-08-18 09:43:03

5

$(".thisClass[id!='thisId']").doAction();

文檔:http://api.jquery.com/category/selectors/

+5

你的意思是$(「。thisClass [id!='thisId']」)。doAction(); ? – detay 2011-06-23 11:16:23

+9

代碼示例中是否缺少']? – David 2012-10-26 20:36:55

+0

醜陋____________ – Imad 2016-12-02 09:43:06

25

還是拿。 not()方法

https://api.jquery.com/not/

$(".thisClass").not("#thisId").doAction(); 
+3

'.not()'不是選擇器。這是一個功能。但是也有':not()'選擇器,與其他答案一樣。 – WoIIe 2014-07-29 10:45:26

2

使用.not()方法與選擇的整個元件也是一種選擇。

如果您想直接對該元素執行另一個操作,則此方法可能非常有用。

$(".thisClass").not($("#thisId")[0].doAnotherAction()).doAction(); 
4

您可以使用。不是功能,如下面的例子刪除有一個確切的ID項目,包含特定的詞ID,ID開始一個字,等...看到http://www.w3schools.com/jquery/jquery_ref_selectors.asp瞭解更多信息在jQuery選擇器上。

忽略精確ID:

$(".thisClass").not('[id="thisId"]').doAction(); 

忽略的ID包含單詞 「ID」

$(".thisClass").not('[id*="Id"]').doAction(); 

忽略的ID以 「我」

$(".thisClass").not('[id^="my"]').doAction(); 
4

我將開始只需扔一個JS(ES6)答案,以防有人在尋找它:

Array.from(document.querySelectorAll(".myClass:not(#myId)")).forEach((el,i) => { 
    doSomething(el); 
} 
相關問題