JS / jQuery:隐藏没有特定属性值的元素

时间:2014-11-15 13:10:13

标签: javascript jquery

<div class="picture" picturename="picture of a horse and stuff"></div>

以下代码将有效隐藏属性picturename包含的所有元素&#34; horse&#34;:

$('[picturename*="horse"]').hide();

但是如何隐藏属性picturename DOESNT包含的所有元素&#34; horse&#34;? 你会想添加一个!在=之前会做的但是没有。

2 个答案:

答案 0 :(得分:3)

$('.picture').not('[picturename*="horse"]').hide();

应该这样做。

答案 1 :(得分:2)

您可以使用替代方案:

&#13;
&#13;
$('.picture:not([picturename*="horse"])').hide();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="picture" picturename="picture of a horse and stuff">show</div>
<div class="picture" picturename="picture of a horse and stuff">show</div>
<div class="picture" picturename="picture of a and stuff">hide</div>
<div class="picture" picturename="picture of a horse and stuff">show</div>
&#13;
&#13;
&#13;

在你最后的评论中隐藏,当你不包含马或狗时,你可以使用:

$('.picture:not([picturename*="horse"]):not([picturename*="dog"])').hide();