jquery选择器 - 选择id不等于的所有span标签
我想选择没有id
的所有HTML <span>
元素等于x
并将它们隐藏起来。这可能吗?我认为你必须使用:not
选择,但我不能工作了:jquery选择器 - 选择id不等于的所有span标签
$('span:not(#x').attr('style', "display: none;");
任何想法?谢谢 :)。
感谢您节省我的一天...... :) –
试试这个
$('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;");
[jquery docs](http://api.jquery.com/not-selector/)现在说:'。与将推荐的选择器或变量推入:not()选择器过滤器相比,not()方法最终会为您提供更多可读的选择。在大多数情况下,这是一个更好的选择 –
$( “跨度”)否( “#X”)隐藏()。 – AutoSponge
看起来你忘了a)。这是否工作? $('span:not(#x)')。attr('style',“display:none;”); – Filip
作为一个快速的附注,如果你只想隐藏元素,使用'$('span:not(#x)')。hide()',将style属性设置为'display:none;'将替换所有现有的内联样式,而'hide()'只会添加'display:none;' - 并显示,只需调用'.show()',只删除'display:none;'样式。 – drovani