CSS css选择器匹配没有属性x的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1533444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:08:41  来源:igfitidea点击:

css selector to match an element without attribute x

css-selectorscss

提问by Stephen Sorensen

I'm working on a CSS file and find the need to style text input boxes, however, I'm running into problems. I need a simple declaration that matches all these elements:

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

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

... but doesn't match these ones:

...但与这些不匹配:

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

Here's what I would like to do:

这是我想做的事情:

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

In the above CSS, notice the first selector is input[!type]. What I mean by this is I want to select all input boxes where the type attribute is not specified (because it defaults to text but input[type='text']doesn't match it). Unfortunately, there is no such selector in the CSS3 spec that I could find.

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

Does anyone know of a way to accomplish this?

有谁知道实现这一目标的方法?

回答by eveliotc

:notselector

:不是选择器

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

Support:in Internet Explorer 9 and higher

支持:在 Internet Explorer 9 及更高版本中

回答by Tim Santeford

For a more cross-browser solution you could style allinputs the way you want the non-typed, text, and password then another style the overrides that style for radios, checkboxes, etc.

对于更跨浏览器的解决方案,您可以按照您想要的非键入、文本和密码的方式设置所有输入的样式,然后另一种样式覆盖收音机、复选框等的样式。

input { border:solid 1px red; }

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

- Or -

- 或者 -

could whatever part of your code that is generating the non-typed inputs give them a class like .no-typeor simply not output at all? Additionally this type of selection could be done with jQuery.

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

回答by smets.kevin

Just wanted to add to this, you can have the :not selector in oldIE using selectivizr: http://selectivizr.com/

只是想添加到此,您可以使用 selectivizr 在 oldIE 中使用 :not 选择器:http://selectivizr.com/