2011-12-01 223 views
4

我想選擇沒有id的所有HTML <span>元素等於x並將它們隱藏起來。這可能嗎?我認爲你必須使用:not選擇,但我不能工作了:jquery選擇器 - 選擇id不等於的所有span標籤

$('span:not(#x').attr('style', "display: none;");

任何想法?謝謝 :)。

+2

$( 「跨度」)否( 「#X」)隱藏()。 – AutoSponge

+1

看起來你忘了a)。這是否工作? $('span:not(#x)')。attr('style',「display:none;」); – Filip

+0

作爲一個快速的附註,如果你只想隱藏元素,使用'$('span:not(#x)')。hide()',將style屬性設置爲'display:none;'將替換所有現有的內聯樣式,而'hide()'只會添加'display:none;' - 並顯示,只需調用'.show()',只刪除'display:none;'樣式。 – drovani

回答

11

你剛纔忘了)。

我做了一個小提示向你展示。 http://jsfiddle.net/3U8tD/

$('span:not(#x)').attr('style', "display: none;"); 
+0

感謝您節省我的一天...... :) –

4
$('span[ID!="x"]').attr('style', "display: none;"); 

設置所有span style屬性,至極具有的ID X,無人能及。

希望這有助於

+1

應該是$('span [ID!=「x」]')。attr('style',「display:none;」); – Filip

+0

對,我忘了字符;)謝謝 – dknaack

1

試試這個

$('span[id!="x"]').attr('style', "display: none;"); 
3

至少有三種方法可以滿足您的需求。

$('span:not(#x)').attr('style', "display: none;"); 
$('span[id!="x"]').attr('style', "display: none;"); 
$('span').filter(':not(#x)').attr('style', "display: none;"); 

最有效的方法是$('span[id!="x"]').attr('style', "display: none;");

http://jsfiddle.net/xpAeK/

+0

[jquery docs](http://api.jquery.com/not-selector/)現在說:'。與將推薦的選擇器或變量推入:not()選擇器過濾器相比,not()方法最終會爲您提供更多可讀的選擇。在大多數情況下,這是一個更好的選擇 –