jQuery选择器 - 查找没有指定属性的对象

时间:2009-12-28 11:57:15

标签: javascript jquery css-selectors

我在页面上有几个对象,我想只在其中一些上使用jQuery执行操作 - 那些没有指定属性的操作。所以:

<li style='...'>some text</li>
<li style='...'>some other text</li>
<li>some very diffrent text</li>

在javascript中我会:

$('li[style]').hide();

会隐藏具有style属性的所有元素。但是如果我想隐藏那些而没有它,我的选择器应该怎么样?

4 个答案:

答案 0 :(得分:74)

jQuery("li:not([style])").hide();

答案 1 :(得分:5)

您可以使用:not psuedo-selector来查找匹配某个选择器的元素。在您的示例中,您要选择没有style属性的所有li元素,因此您将使用以下内容:

$('li:not([style])').hide();

您也可以将其与其他选择器结合使用:

$('#div input:not(:checked)').show();

这是一个非常强大的工具!

答案 2 :(得分:1)

尝试:

$('li:not([style])').hide();

答案 3 :(得分:0)

您可能会发现使用.not()方法而不是选择器更清晰,更高效。举个例子:

$('li').not('[style]').hide();

这也是jQuery开发者recommend