简体   繁体   English

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

[英]JS/jQuery : Hiding elements which doesnt have a spesific attribute value

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

The code below will effectively hide all elements where attribute picturename contains "horse": 下面的代码将有效地隐藏属性picturename包含“马”的所有元素:

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

But how do I hide all elements where attribute picturename DOESNT contain "horse"? 但是,如何隐藏属性图片名称DOESNT包含“马”的所有元素? You would think adding a ! 您可能会想添加一个! before the = would do the trick but no. 在=之前可以解决问题,但不能。

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

这应该做。

Alternative you can use: 您可以使用的替代方法:

 $('.picture:not([picturename*="horse"])').hide(); 
 <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> 

After your last comment to hide when doesn't contain horse or dog you can use: 在隐藏不包含马或狗的地方的最后一条评论之后,您可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取具有值为val的属性attr的元素数组 - How to get an array of elements which have an attribute attr with value val Ext JS获取行特定单元格字符串值的前4个元素 - Ext js get row spesific cell string value's first 4 elements 如何使用jquery仅将那些没有任何价值的文本框设为只读? - How to make only those textboxes readonly which doesnt have any value in it,using jquery? 查找Backbone集合中没有属性的模型 - Find model which doesnt have an attribute in Backbone collection 如何使用jquery查找并选择具有以特定单词开头的数据属性的所有元素? - How can I find and select all elements which have data attribute start with specific word with using jquery? 如何使用jQuery获取大于给定值的元素的数据属性值? - How to get data-attribute value of elements which is greater than given value using jquery? 是否可以将具有特定值的所有元素放入任何类型的属性中? - Is it possible to get all elements which have a specific value into any type of attribute? 用JS隐藏元素 - Hiding elements with JS jQuery添加类,并为所有没有该类的元素将其删除 - jQuery add class and remove it for all the elements who doesnt have it jQuery:计算那些没有属性的元素 - jQuery: count those elements that have no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM