[英]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.