我正在处理一个CSS文件,并发现需要样式文本输入框,但是,我遇到了问题。我需要一个简单的声明来匹配所有这些元素:

<input />
<input type='text' />
<input type='password' />

... 但与下面这些不匹配:

<input type='submit' />
<input type='button' />
<input type='image' />
<input type='file' />
<input type='checkbox' />
<input type='radio' />
<input type='reset' />

这是我想做的:

input[!type], input[type='text'], input[type='password'] {
   /* styles here */
}

在上面的CSS中,请注意第一个选择器是input[!type]。我的意思是,我想选择所有没有指定类型属性的输入框(因为它默认为文本,但input[type='text']不匹配它)。不幸的是,我在CSS3规范中找不到这样的选择器。

有人知道怎么做到吗?


:不选择器:

input:not([type]), input[type='text'], input[type='password'] {
    /* style here */
}

支持:Internet Explorer 9及以上版本


对于一个更跨浏览器的解决方案,你可以按照你想要的方式对所有输入设置样式——非类型化的、文本的和密码的,然后用另一种样式重写无线电、复选框等的样式。

input { border:solid 1px red; }

input[type=radio], 
input[type=checkbox], 
input[type=submit], 
input[type=reset], 
input[type=file] 
      { border:none; }

-或-

代码中生成非类型化输入的部分是否可以给它们一个类似.no-type的类,或者根本不输出?此外,这种类型的选择可以用jQuery完成。


只是想补充一点,你可以在oldIE中使用selectivizr: http://selectivizr.com/有:not选择器