可以:not()伪类有多个参数吗?

del*_*phi 726 css css-selectors css3

我正在尝试选择除了和之外input的所有元素.typeradiocheckbox

很多人已经表明你可以放入多个参数:not,但是使用type似乎无论如何都没有用到我试试.

form input:not([type="radio"], [type="checkbox"]) {
  /* css here */
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Fel*_*ing 1488

原因:不只是使用两个:not:

input:not([type="radio"]):not([type="checkbox"])
Run Code Online (Sandbox Code Playgroud)

是的,这是有意的

  • 对于那些没有得到幽默的人:他用":"字符说"为什么不......". (49认同)
  • 作为旁注,如果你执行类似`input:not('.c1')的操作,输入:not('c2')`你最终得到一个"和"情况,其中两个类都必须在输入上它匹配. (11认同)
  • 这具有很高的特异性。 (3认同)
  • @BoltClock延迟,但是,因此也希望`:not([attr],[attr])`CSV格式:-P (3认同)
  • @Cloudkiller否,它将选择任何输入元素->“不带类c1的输入或不带类c2的输入” (2认同)
  • @DavidCallanan 你认为对吗?但它确实起到了 AND 的作用。不太明白为什么。 (2认同)

Dan*_*non 46

如果您在项目中使用SASS,我已经构建了这个mixin,使其按照我们想要的方式工作:

@mixin not($ignorList...) {
    //if only a single value given
    @if (length($ignorList) == 1){
        //it is probably a list variable so set ignore list to the variable
        $ignorList: nth($ignorList,1);
    }
    //set up an empty $notOutput variable
    $notOutput: '';
    //for each item in the list
    @each $not in $ignorList {
        //generate a :not([ignored_item]) segment for each item in the ignore list and put them back to back
        $notOutput: $notOutput + ':not(#{$not})';
    }
    //output the full :not() rule including all ignored items
    &#{$notOutput} {
        @content;
    }
}
Run Code Online (Sandbox Code Playgroud)

它可以以两种方式使用:

选项1:列出内联忽略的项目

input {
  /*non-ignored styling goes here*/
  @include not('[type="radio"]','[type="checkbox"]'){
    /*ignored styling goes here*/
  }
}
Run Code Online (Sandbox Code Playgroud)

选项2:首先列出变量中被忽略的项目

$ignoredItems:
  '[type="radio"]',
  '[type="checkbox"]'
;

input {
  /*non-ignored styling goes here*/
  @include not($ignoredItems){
    /*ignored styling goes here*/
  }
}
Run Code Online (Sandbox Code Playgroud)

两个选项的输出CSS

input {
    /*non-ignored styling goes here*/
}

input:not([type="radio"]):not([type="checkbox"]) {
    /*ignored styling goes here*/
}
Run Code Online (Sandbox Code Playgroud)

  • 是不是有点像要求伐木工人去五金店取代他的木头? (6认同)
  • 在效率方面:[是的.](http://puu.sh/kkfuB/93b41e8496.png)方式少了''`字符和imo更有效的代码. (2认同)
  • @DaanHeskes还写出了所有:not()案例不允许你使用变量来列出它们. (2认同)

Pie*_*one 30

从CSS 4开始,:not可以在选择器中使用多个参数(参见此处).

在CSS3中,:not selector仅允许1个选择器作为参数.在第4级选择器中,它可以将选择器列表作为参数.

例:

/* In this example, all p elements will be red, except for 
   the first child and the ones with the class special. */

p:not(:first-child, .special) {
  color: red;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,浏览器支持有限.目前,它只适用于Safari.

  • 请记住它的CSS Selectors Level 4,而不是CSS 4,没有CSS 4这样的东西,可能永远不会存在. (7认同)
  • 根据[caniuse.com](https://caniuse.com/#search=negation),它仍然仅受Safari支持 (3认同)
  • 现在它适用于所有主要浏览器。这应该是 2021 年公认的答案。 (2认同)

小智 8

我遇到了一些麻烦,"X:not():not()"方法对我不起作用.

我最终诉诸于这个策略:

INPUT {
    /* styles */
}
INPUT[type="radio"], INPUT[type="checkbox"] {
    /* styles that reset previous styles */
}
Run Code Online (Sandbox Code Playgroud)

它不是那么有趣,但它适用于我:not()是好斗的.它并不理想,但它很坚固.


Dan*_*non 6

如果您安装了“cssnext”Post CSS 插件,那么您可以安全地开始使用您现在想要使用的语法。

使用 cssnext 会变成这样:

input:not([type="radio"], [type="checkbox"]) {
  /* css here */
}
Run Code Online (Sandbox Code Playgroud)

进入这个:

input:not([type="radio"]):not([type="checkbox"]) {
  /* css here */
}
Run Code Online (Sandbox Code Playgroud)

https://cssnext.github.io/features/#not-pseudo-class