jquery选择器 - 选择id不等于的所有span标签

jquery选择器 - 选择id不等于的所有span标签

问题描述:

我想选择没有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

你刚才忘了)。

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

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

感谢您节省我的一天...... :) –

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

设置所有span style属性,至极具有的ID X,无人能及。

希望这有助于

+1

应该是$('span [ID!=“x”]')。attr('style',“display:none;”); – Filip

+0

对,我忘了字符;)谢谢 – dknaack

试试这个

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

至少有三种方法可以满足您的需求。

$('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()方法最终会为您提供更多可读的选择。在大多数情况下,这是一个更好的选择 –