简体   繁体   English

在 CSS 中指定多个属性选择器

[英]Specify multiple attribute selectors in CSS

What is the syntax for doing something like:执行以下操作的语法是什么:

input[name="Sex" AND value="M"]

Basically, I want to select the input element that has the attribute name="Sex" as well as the attribute value="M" :基本上,我想 select 具有属性name="Sex"以及属性value="M"input元素:

<input type="radio" name="Sex" value="M" />

Elements such as the following should not be selected:不应选择以下元素:

<input type="radio" name="Sex" value="F" />

Simple input[name=Sex][value=M] would do pretty nice.简单的input[name=Sex][value=M]会很不错。 And it's actually well-described in the standard doc :它实际上在标准文档中有很好的描述:

Multiple attribute selectors can be used to refer to several attributes of an element, or even several times to the same attribute.多个属性选择器可用于引用元素的多个属性,甚至多次引用同一属性。

Here, the selector matches all SPAN elements whose "hello" attribute has exactly the value "Cleveland" and whose "goodbye" attribute has exactly the value "Columbus":在这里,选择器匹配所有“hello”属性的值恰好为“Cleveland”并且其“goodbye”属性的值恰好为“Columbus”的 SPAN 元素:

span[hello="Cleveland"][goodbye="Columbus"] { color: blue; }

As a side note, using quotation marks around an attribute value is required only if this value is not a valid identifier.作为旁注,仅当该值不是有效标识符时才需要在属性值周围使用引号。

JSFiddle Demo JSFiddle 演示

For concatenating it's:连接它是:

input[name="Sex"][value="M"] {}

And for taking union it's:而对于联合来说,它是:

input[name="Sex"], input[value="M"] {}

连接属性选择器:

input[name="Sex"][value="M"]

Just to add that there should be no space between the selector and the opening bracket.只是要补充一点,选择器和左括号之间不应该有空格。

td[someclass] 

will work.将工作。 But

td [someclass] 

will not.将不会。

[class*="test"],[class="second"] {
background: #ffff00;
}

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

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