是否有 2 个或更多具有相同属性值的元素的选择器?

2022-10-19前端开发问题
6

本文介绍了是否有 2 个或更多具有相同属性值的元素的选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有没有更优雅的写法?

.standard {填充顶部:50px;底部填充:50px;}.standard.color-0 + .standard.color-0,.standard.color-1 + .standard.color-1,.standard.color-2 + .standard.color-2,.standard.color-3 + .standard.color-3,.standard.color-4 + .standard.color-4,.standard.color-5 + .standard.color-5,.standard.color-6 + .standard.color-6,.standard.color-7 + .standard.color-7,.standard.color-8 + .standard.color-8 {填充顶部:0;}

是否有一些选择器可以检查在 2 个或更多元素上找到的类的匹配项,而实际上并不知道确切的类名?比如这样的:

.standard.color-* + .standard.color-* {填充顶部:0;}

我目前所拥有的(上面发布的)按照我想要的方式在我的网站上显示,但我只是好奇我是否注定要不断添加 .standard.color-# + .standard.color-# 用于我需要的每种新颜色(在这种情况下是全宽 <section> 标签的背景颜色).p>

例子:

<section class="standard color-0"></section>//顶部和底部填充<section class="标准颜色-1"></section>//顶部和底部填充-----------------------------------------------------------------------<section class="标准颜色-1"></section>//顶部和底部填充<section class="标准颜色-1"></section>//padding-top: 0;(如果两个color-#"完全相同,则会丢失其顶部填充)

简化的帖子和代码.<section> 将始终有一个 .standard 类和一个带有 .color-0.color- 类正在 background-color: transparent;.

解决方案

不幸的是,由于选择器的静态特性,CSS 没有提供一种方法让一个复合选择器引用另一个复合选择器的任何部分,即使使用一种类似正则表达式的语法.因此,例如,在两个复合选择器中,如果不对实际值进行硬编码,就不能匹配具有与其前一个兄弟相同的类名或属性值的元素.唯一的解决方案是您拥有的解决方案.

正如我在对上面链接的问题的回答中提到的,如果您使用的是预处理器,则可以稍微自动化一下.它仍然会在 CSS 中产生相同的硬编码选择器,但实际编写这些选择器的任务被卸载到预处理器上.这是一个使用 SCSS 的示例:

.standard {填充顶部:50px;底部填充:50px;&% 连续 {填充顶部:0;}//要容纳更多编号的类,只需编辑此循环@for $i 从 0 到 8 {&.color-#{$i} + &.color-#{$i} {@extend % 连续;}}}

同样,这需要提前知道这些值.如果你不能写下所有可能的值(或者你不想写),你需要编写一个脚本来检查运行时的值.

Is there a more elegant way to write this?

.standard {
  padding-top: 50px;
  padding-bottom: 50px;
}
.standard.color-0 + .standard.color-0,
.standard.color-1 + .standard.color-1,
.standard.color-2 + .standard.color-2,
.standard.color-3 + .standard.color-3,
.standard.color-4 + .standard.color-4,
.standard.color-5 + .standard.color-5,
.standard.color-6 + .standard.color-6,
.standard.color-7 + .standard.color-7,
.standard.color-8 + .standard.color-8 {
  padding-top: 0;
}

Is there perhaps some selector that checks for matches of the classes found on 2 or more elements without actually knowing the exact class's name? Such as something like:

.standard.color-* + .standard.color-* {
  padding-top: 0;
}

What I have currently (posted above) works the way I want it to as far as how it displays on my site, but I am just curious whether, or not, I am doomed to constantly add .standard.color-# + .standard.color-# for every new color I need (which in for this case are background-colors for full-width <section> tags).

Examples:

<section class="standard color-0"></section> // top and bottom padding
<section class="standard color-1"></section> // top and bottom padding

-----------------------------------------------------------------------

<section class="standard color-1"></section> // top and bottom padding
<section class="standard color-1"></section> // padding-top: 0; (if both "color-#" is the exact same this loses its top padding)

EDIT: Simplified post and code. <section> will always have a .standard class and a .color- class with .color-0 being background-color: transparent;.

解决方案

Unfortunately, due to the static nature of selectors, CSS doesn't offer a way for one compound selector to reference any part of another compound selector, not even with a regex-like syntax. So you can't, for example, match an element with the same class name or attribute value as its previous sibling without hardcoding the actual value, in both compound selectors. The only solution is the one you have.

As I mention in my answer to the question linked above, if you're using a preprocessor, you can automate this somewhat. It will still result in the same hardcoded selectors in CSS, but the task of actually writing those selectors is offloaded to the preprocessor instead. Here's an example using SCSS:

.standard {
  padding-top: 50px;
  padding-bottom: 50px;

  &%consecutive {
    padding-top: 0;
  }

  // To accommodate more numbered classes simply edit this loop
  @for $i from 0 through 8 {
    &.color-#{$i} + &.color-#{$i} {
      @extend %consecutive;
    }
  }
}

This, again, requires knowing the values in advance. If you cannot write down all the possible values (or you don't want to), you'll need to write a script to examine the values in runtime.

这篇关于是否有 2 个或更多具有相同属性值的元素的选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6