本文翻译自:Can I write a CSS selector selecting elements NOT having a certain class or attribute?

I would like to write a CSS selector rule that selects all elements that don't have a certain class. 我想编写一个CSS选择器规则,选择所有没有特定类的元素。 For example, given the following HTML: 例如,给定以下HTML:

<html class="printable"><body class="printable"><h1 class="printable">Example</h1><nav><!-- Some menu links... --></nav><a href="javascript:void(0)" onclick="javascript:self.print()">Print me!</a><p class="printable">This page is super interresting and you should print it!</p></body>
</html>

I would like to write a selector that selects all elements that don't have the "printable" class which, in this case, are the nav and a elements. 我想编写一个选择器来选择所有没有“可打印”类元素,在这种情况下,它是nav和元素。

Is this possible? 这可能吗?

NOTE: in the actual HTML where I would like to use this, there are going to be a lot more elements that don't have the "printable" class than do (I realize it's the other way around in the above example). 注意:在我想要使用它的实际HTML中,将会有更多的元素没有 “可打印”类而不是(我在上面的例子中意识到它是另一种方式)。


#1楼

参考:https://stackoom.com/question/cE0K/我可以编写一个CSS选择器来选择不具有某个类或属性的元素吗


#2楼

You can use :not(.class) selector as mentioned before. 您可以使用:not(.class)选择器,如前所述。

If you care about Internet explorer compatibility I recommend you to use http://selectivizr.com/ . 如果您关心Internet Explorer兼容性,我建议您使用http://selectivizr.com/ 。

But remember to run it under apache otherwise you won't see the effect. 但是记得在apache下运行它,否则你将看不到效果。


#3楼

:not([class])

Actually, this will select anything that does not have a css class ( class="css-selector" ) applied to it. 实际上,这将选择任何没有应用css类( class="css-selector" )的东西。

I made a jsfiddle demo 我做了一个jsfiddle演示

  h2 {color:#fff} :not([class]) {color:red;background-color:blue} .fake-class {color:green} 
  <h2 class="fake-class">fake-class will be green</h2> <h2 class="">empty class SHOULD be white</h2> <h2>no class should be red</h2> <h2 class="fake-clas2s">fake-class2 SHOULD be white</h2> <h2 class="">empty class2 SHOULD be white</h2> <h2>no class2 SHOULD be red</h2> 

Is this supported? 这支持吗? Yes : Caniuse.com (accessed 25 Aug 2014) : 是:Caniuse.com(2014年8月25日访问) :

  • Support: 88.85% 支持率:88.85%
  • Partial support: 4.36% 部分支持:4.36%
  • Total:93.21% 总计:93.21%

Funny edit, I was Googling for the opposite of :not. 有趣的编辑,我是谷歌搜索相反:不。 CSS negation? CSS否定?

selector[class]  /* the oposite of :not[]*/

#4楼

The :not negation pseudo class :not否定伪类

The negation CSS pseudo-class, :not(X) , is a functional notation taking a simple selector X as an argument. 否定CSS伪类, :not(X) ,是一个以简单的选择器X作为参数的功能表示法。 It matches an element that is not represented by the argument. 它匹配一个未由参数表示的元素。 X must not contain another negation selector. X不得包含另一个否定选择器。

You can use :not to exclude any subset of matched elements, ordered as you would normal CSS selectors. 您可以使用:not排除匹配元素的任何子集,按照普通CSS选择器的顺序排序。


Simple example: excluding by class 简单示例:按类排除

div:not(.class)

Would select all div elements without the class .class 将选择没有类.class所有div元素

 div:not(.class) { color: red; } 
 <div>Make me red!</div> <div class="class">...but not me...</div> 

Complex example: excluding by type / hierarchy 复杂示例:按类型/层次结构排除

:not(div) > div

Would select all div elements which arent children of another div 将选择所有div元素不是另一个div子元素

 div { color: black } :not(div) > div { color: red; } 
 <div>Make me red!</div> <div> <div>...but not me...</div> </div> 

Complex example: chaining pseudo selectors 复杂的例子:链接伪选择器

With the notable exception of not being able to chain/nest :not selectors and pseudo elements, you can use in conjunction with other pseudo selectors. 除了无法链/嵌套之外的明显例外:not选择器和伪元素,您可以与其他伪选择器一起使用。

 div { color: black } :not(:nth-child(2)){ color: red; } 
 <div> <div>Make me red!</div> <div>...but not me...</div> </div> 

Browser Support , etc. 浏览器支持等

:not is a CSS3 level selector , the main exception in terms of support is that it is IE9+ :not CSS3级别的选择器 ,支持方面的主要例外是它是IE9 +

The spec also makes an interesting point: 该规范也提出了一个有趣的观点:

the :not() pseudo allows useless selectors to be written. :not()伪允许写入无用的选择器。 For instance :not(*|*) , which represents no element at all, or foo:not(bar) , which is equivalent to foo but with a higher specificity. 例如:not(*|*) ,它根本不代表任何元素,或者foo:not(bar) ,它相当于foo但具有更高的特异性。


#5楼

Just like to contribute that the above answers of :not() can be very effective in angular forms, rather than creating effects or adjusting the view/DOM, 就像贡献一样,上面的答案:not()在角形式中非常有效,而不是创建效果或调整视图/ DOM,

input.ng-invalid:not(.ng-pristine) { ... your css here i.e. border-color: red; ...}

Ensures that on loading your page, the input fields will only show the invalid (red borders or backgrounds, etc) if they have data added (ie no longer pristine) but are invalid. 确保在加载页面时,输入字段仅显示无效(红色边框或背景等),如果它们已添加数据(即不再是原始数据)但无效。


#6楼

Example

  [class*='section-']:not(.section-name) {@include opacity(0.6);// Write your css code here}

// Opacity 0.6 all "section-" but not "section-name" //不透明度0.6所有“部分 - ”但不是“部分名称”

我可以编写一个CSS选择器来选择不具有某个类或属性的元素吗?相关推荐

  1. 编写一个Applet,随机选择矩形、圆形、椭圆、直线等形状,在可视区域内绘制20个图形,同一种图形使用同一种颜色,不需要填充。

    编写一个Applet,随机选择矩形.圆形.椭圆.直线等形状,在可视区域内绘制20个图形,同一种图形使用同一种颜色,不需要填充. package p2;import java.applet.Applet ...

  2. html连在一起的选择器,请问关于css选择器的问题:两个类选择器连在一起是什么意思?...

    CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 选择器包括单独的选择和分组选择. 如:h1 { color:red;} 单独选择器 定义h1标签的文字颜色 h1, h2, h3, h4 ...

  3. 爬虫css选择器,选择属性

    有一个网页是这样的,我向提取onclick里的值. 这时用attr, 如下代码: if selector.css(".con10 a.but_alert").get():id = ...

  4. css 选择器 如何 选择 大于 N 的情况 ?第n个元素之后的情况

    我们可以 通过 nth-child 选择  第几个子元素   但是要做到  大于 第n的元素 怎么做的 其实 我们可以通过配合 实现呢 假设 我把大于 第二个元素  也就是从第三个开始  都是红色  ...

  5. css选择器(选择div内所有p元素)

    情况1:<div><p></div> 情况2:<div><a><p></p></a></div&g ...

  6. 编写一个可动态注入Spring 容器的工具类

    导语   在有些使用场景下,我们使用Bean的时候发现它并没有那么必要在这个类中注入对应的对象引用.就有一个需求,就是再需要的地方将指定的类注入到容器中,这样这个类就可以动态的使用到原本已经在容器中的 ...

  7. 用JAVA编写一个水果箱,Java SE 第四章类之间的关系--水果箱编程题

    问题:有一个水果箱(Box),箱子里装有水果(Fruit),每一种水果都有不同的重量和颜色,水果有:苹果.梨.桔子.每个苹果(Apple)都有不同的重量和颜色,每个桔子(Orange)都有不同的重量和 ...

  8. 编写一个函数mysum,以整数列表为参数,返回所以元素之和。

    def mysum(List): sum=0 for item in List: sum+=item return sum List=[1,2,3] res=mysum(List) print(res ...

  9. css选择器匹配没有属性x的元素[重复]

    本文翻译自:css selector to match an element without attribute x [duplicate] This question already has an ...

最新文章

  1. 查找计算机 域服务不可用,win7系统打印文件提示Active Directory域服务不可用解决方法...
  2. spring-boot的access日志格式修改
  3. JS性能--DOM编程之重排与重绘
  4. 2021广东省高考成绩查询时间,广东省高考成绩查询时间及方式公布
  5. MT7628/MT7688 修改串口2作为调试串口 所踩的坑
  6. 三分钟玩转jQuery.noConflict()
  7. html防替换资源,Webpack中有没有替换html静态资源的插件
  8. java/05/(Swing包)窗体,组件,布局管理器,面板,监听事件
  9. SQL必知必会-过滤数据
  10. URL转码escape() encodeURI() encodeURIComponent()
  11. Linux防火墙端口设置和mysql端口开放的navicat整合
  12. cshop是什么开发语言_C语言是用什么语言编写出来的?
  13. python任务栏都隐藏了_请问如何始终隐藏WINDOWS任务栏?
  14. 全国高中数学联赛——数论
  15. Java常见面试题含答案(第一期)
  16. leetcode 605 种花问题 (c++和python)
  17. FMDB的简单应用(4篇)
  18. CentOS7上Glusterfs的安装及使用(gluster/heketi)
  19. 常见的SNS盈利模式(商业模式)
  20. Happy Father's Day 告诉父亲你爱他

热门文章

  1. Servlet--ServletInputStream类,ServletOutputStream类
  2. 【编译打包】nginx_1.6.2-1~precise.debian.tar.gz
  3. 01-C#入门(分支控制语句)
  4. java类、抽象类、接口的继承规则
  5. With语句是什么?
  6. stm32中的延时函数
  7. 如何设计自适应屏幕大小的网页(转)
  8. Haar-like特征来龙去脉
  9. weblogic时间问题
  10. zabbix server下数据库日志报错