CSS输入类型选择器 - 可能具有"或"或"非"语法?

RPM*_*984 93 html css css-selectors

如果它们存在于编程中),

如果我有一个包含以下输入的HTML表单:

<input type="text" />
<input type="password" />
<input type="checkbox" />
Run Code Online (Sandbox Code Playgroud)

我想将样式应用于所有type="text"或其中的输入type="password".

或者,我会满足所有输入的位置type != "checkbox".

看起来我必须这样做:

input[type='text'], input[type='password']
{
   // my css
}
Run Code Online (Sandbox Code Playgroud)

有没有办法:

input[type='text',type='password']
{
   // my css
}
Run Code Online (Sandbox Code Playgroud)

要么

input[type!='checkbox']
{
   // my css
}
Run Code Online (Sandbox Code Playgroud)

我环顾四周,似乎没有办法用一个CSS选择器来做到这一点.

当然不是很重要,但我只是一只好奇的猫.

有任何想法吗?

Pat*_*ney 173

CSS3有一个名为:not()的伪类

input:not([type='checkbox']) {    
    visibility: hidden;
}
Run Code Online (Sandbox Code Playgroud)
<p>If <code>:not()</code> is supported, you'll only see the checkbox.</p>
    	                              
<ul>
  <li>text: (<input type="text">)</li>  
  <li>password (<input type="password">)</li>    	
  <li>checkbox (<input type="checkbox">)</li> 
 </ul>
Run Code Online (Sandbox Code Playgroud)


多个选择器

正如文森特所提到的,可以将多个串联:not()在一起:

input:not([type='checkbox']):not([type='submit'])
Run Code Online (Sandbox Code Playgroud)

CSS4 尚未得到广泛支持,它允许多个选择器:not()

input:not([type='checkbox'],[type='submit'])
Run Code Online (Sandbox Code Playgroud)


遗产支持

所有现代浏览器都支持CSS3语法.当问到这个问题时,我们需要对IE7和IE8进行回退.一种选择是使用像IE9.js这样的polyfill.另一个是利用CSS中的级联:

input {
   // styles for most inputs
}   

input[type=checkbox] {
  // revert back to the original style
} 

input.checkbox {
  // for completeness, this would have worked even in IE3!
} 
Run Code Online (Sandbox Code Playgroud)

  • 为了完整起见,如果你想做多个"不",那么这就是要使用的语法:input:not([type ='checkbox']):not([type ='submit']) (12认同)

cod*_*keh 23

input[type='text'], input[type='password']
{
   // my css
}
Run Code Online (Sandbox Code Playgroud)

这是正确的方法.遗憾的是,CSS不是一种编程语言.

  • 不过,您可以使用Less CSS或Sass. (4认同)
  • 那好吧.也许CSS4?=) (2认同)