【问题标题】:Regex-like CSS Attribute Selector Wildcard Capture类似正则表达式的 CSS 属性选择器通配符捕获
【发布时间】:2015-10-27 06:34:15
【问题描述】:

假设我有以下 HTML:

<div class="tocolor-red">    tocolor </div>
<div class="tocolor-blue">   tocolor </div>
<div class="tocolor-green">  tocolor </div>
<div class="tocolor-yellow"> tocolor </div>

不必为每种颜色重复下面的 sn-p 之类的 CSS 代码...

.tocolor-red{
  background: red;
}
.tocolor-red::before {
  content: "red";
}

...有没有办法编写一个从 css 类中捕获颜色的 CSS 规则?为了尽职调查,我已经查找了属性选择器,并且我注意到已经有多种方法可以在 css 中使用通配符。但是,我还没有找到任何可以让我捕获通配符文本并将其作为规则一部分的内容。

如果正则表达式适用于 CSS,则规则如下所示:

.tocolor-([\w+]) {
   background: $1;
}
.tocolor-([\w+]:before {
   content: $1;
}

【问题讨论】:

  • 您实际上不能将 html 属性的属性值用作 css 规则的值,但使用 sass 或更少可以生成循环中所需的每个规则
  • 这是个好主意,用 JS 很容易做到。
  • Css 并不能真正做到这一点。正如您已经建议的那样,您可能需要编译为 css(less、sass、scss)的语言,但是您必须为每种颜色添加规则。
  • @cimmanon 一个 sass 示例 :) blackfalcon.roughdraft.io/… 所以它会生成您需要的规则,但当然它不会从 html 中提取任何内容。只需使用有效的方法或正确的工具;)

标签: javascript css


【解决方案1】:

CSS 无法支持正则表达式捕获,虽然您可以选择类以字符串 tocolor- 开头的所有元素,但 CSS 无法捕获字符串的值以将其应用于规则。

顺便说一句,有点晚了,用 JavaScript 做到这一点的一种方法:

// retrieve all elements containing the string 'tocolor-':
var elements = document.querySelectorAll('[class*="tocolor-"]'),

// declaring two variables for use in later loops:
    classes, colorString;

// iterating over the (Array-like) collection of elements,
// using Function.prototype.call() to be able to apply the
// Array.prototype.forEach() method on the collection:
Array.prototype.forEach.call(elements, function (elem) {
    // 'elem' is the individual node of the collection
    // over which we're iterating.

    // creating an Array of the classes from the element:
    classes = Array.prototype.slice.call(elem.classList, 0);

    // creating a new Array, using Array.prototype.map():
    colorString = classes.map(function (c) {
        // 'c' is the specific class of the Array of classes
        // over which we're iterating.

        // if the current class ('c') begins with the string
        // 'tocolor-' then we return that class
        if (c.indexOf('tocolor-') === 0) {

            // after first replacing the 'tocolor-' string
            // with an empty string:
            return c.replace('tocolor-','');
        }
    });

    // setting the color of the element ('elem') to
    // the string found:
    elem.style.color = colorString;
});

var elements = document.querySelectorAll('[class*="tocolor-"]'),
  classes, colorString;
Array.prototype.forEach.call(elements, function(elem) {
  classes = Array.prototype.slice.call(elem.classList, 0);
  colorString = classes.map(function(c) {
    if (c.indexOf('tocolor-') === 0) {
      return c.replace('tocolor-', '');
    }
  });
  elem.style.color = colorString;
});
<div class="tocolor-red">tocolor</div>
<div class="tocolor-blue">tocolor</div>
<div class="tocolor-green">tocolor</div>
<div class="tocolor-yellow">tocolor</div>

【讨论】:

  • 很遗憾,我对 SASS 无能为力,因为我从未真正使用过它。
【解决方案2】:

这是你的通配符:

div[class^='.tocolor-'] {
    background: red;
}

但是如果你想使用变量,你应该看看 SASS:

http://sass-lang.com/

【讨论】:

  • 啊,但如果我想让tocolor-blue 显示蓝色而不需要编写第二条规则怎么办?
  • 如果你想在选择器上使用变量,你应该看看 SASS:sass-lang.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 2011-10-29
  • 2010-09-08
  • 2017-05-27
  • 2022-06-11
  • 2012-06-25
相关资源
最近更新 更多