2009-10-07 80 views
211

我正在处理一个CSS文件,并发现需要设置文本输入框的样式,但是,我遇到了问题。我需要所有这些元素相匹配的简单声明:css选择器以匹配没有属性的元素x

<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规范中没有我能找到的选择器。

有谁知道一种方法来完成这个?

回答

345

:不选择

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

Support:在Internet Explorer 9和更高

+11

这是不支持IE6,IE7和可能不是在IE8 – 2009-10-07 19:08:52

+130

@Tim,话又说回来了,_is_支持哪些:使用selectivizr在过时的歌曲没有选择器在IE6,7和8 ... – priestc 2009-10-07 19:36:43

+1

@ nbv4,真正的dat,但看到我的答案。 – 2009-10-07 19:49:55

26

对于更多的跨浏览器解决方案,你可以样式所有输入你想要的非类型化的方式,文本和密码,然后另一个样式覆盖收音机,复选框等的样式。

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来完成。

+0

聪明的解决方案。将不得不更新为新的html5类型(例如“电子邮件”) – TimoSolo 2012-07-25 14:12:42

+5

这是不一样的;例如,如果您删除边框,Opera会从输入中移除它的默认样式。 – feeela 2013-01-25 14:12:14