del*_*phi 726 css css-selectors css3
我正在尝试选择除了和之外input
的所有元素.type
radio
checkbox
很多人已经表明你可以放入多个参数: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)
是的,这是有意的
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)
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.
小智 8
我遇到了一些麻烦,"X:not():not()"方法对我不起作用.
我最终诉诸于这个策略:
INPUT {
/* styles */
}
INPUT[type="radio"], INPUT[type="checkbox"] {
/* styles that reset previous styles */
}
Run Code Online (Sandbox Code Playgroud)
它不是那么有趣,但它适用于我:not()是好斗的.它并不理想,但它很坚固.
如果您安装了“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