简体   繁体   English

如何选择必须具有多个CSS类的元素?

[英]How to select an element that must have more than one css class?

I have an element that has two classes at all times: 我有一个始终具有两个类的元素:

<p class="general_description laptop">
<p class="images laptop">
<p class="specs laptop">

One class describes the item ( laptop ) and the other ( general_description, images, specs ) describes the state that the page needs to be in to present one of three types of information about the item. 一类描述了项目( 笔记本电脑 ),另一类( general_description,图像,specs )描述了页面需要呈现的状态以显示关于该项目的三种信息之一。

How can I show or hide the <p> element only if both classes in my selector are present? 仅当选择器中的两个类都存在时,才如何显示或隐藏<p>元素?

For example, if I only want to show the <p> element that corresponds to both the specs and laptop classes, I have tried doing this but it does not work: 例如,如果我只想显示与specslaptop类相对应的<p>元素,则尝试这样做,但它不起作用:

$(".specs .laptop").show();

$(".specs.laptop").show(); $( “specs.laptop”)显示()。

OR 要么

you could use the is() function in jquery. 您可以在jquery中使用is()函数。 it takes a selector and tells you if the element conforms to it: 它带有一个选择器,并告诉您元素是否符合它:

$(".specs").is(".laptop"); //returns true if the element has the laptop class

IIRC,它是:

$(".specs").filter(".laptop").show()

Get rid of the space between the class names. 消除类名之间的空格。

$(".specs.laptop").show();

Reference: jQuery 参考: jQuery

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM