如何重构某些TD元素的着色?

发布于 2025-02-11 09:18:07 字数 253 浏览 2 评论 0原文

我正在通过声明来着色特定的TD元素:

td[style*="background-color: #ffc"], td[style*="background-color: #fb9"], 
td[style*="background-color: #bfc"], td[style*="background-color: #fcc"] {
background-color: #0d1117 !important;
}

如何重构?

I'm coloring specific td elements by declaring:

td[style*="background-color: #ffc"], td[style*="background-color: #fb9"], 
td[style*="background-color: #bfc"], td[style*="background-color: #fcc"] {
background-color: #0d1117 !important;
}

How can I refactor this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酒忠诚 2025-02-18 09:18:07

CSS中有一个名为:is的伪级,使您删除代码重复

td:is([style*="background-color: #ffc"], [style*="background-color: #fb9"], [style*="background-color: #bfc"], [style*="background-color: #fcc"]) {
    background-color: #0d1117 !important;
}

文档: https://developer.mozilla.orgg/ en-us/doc/web/css/:is


或者您可以使用javascript使用 foreach

它的工作原理:
您有两个变量,
第一个需要一系列十六进制颜色代码。
第二个变量需要您希望将元素颜色的最终颜色。

如何使其在我的项目上工作:

  1. 创建< script>标签或javascript文件。
  2. 将此代码插入其中:
let colorsArray = ["ffc", "fb9", "bfc", "fcc"]; // change this line
let finalColor = "0d1117";

colorsArray.forEach((color) => {
    let tdWithColor = document.querySelectorAll(`td[style*="background-color: #${color}"]`);

    tdWithColor.forEach((element) => {
        element.style = `background-color: #${finalColor}`;
    })
});

// watch the code snippet below if you want comments
  1. 将变量更改为您需要的内容。

带有演示的代码片段示例:

let colorsArray = ["ffc", "fb9", "bfc", "fcc"]; // change this line... don't put "#" in the beginning (js automatically adding it)
let finalColor = "0d1117"; // your final color, don't put "#" in the beginning (js automatically adding it)

/* Don't change the lines below */

/* for every color we are adding this logic below */
colorsArray.forEach((color) => {
  /* getting all the html elements with the same color */
  let tdWithColor = document.querySelectorAll(`td[style*="background-color: #${color}"]`);

  /* if there is more than one td of the same color we are coloring it too */
  tdWithColor.forEach((element) => {
    element.style = `background-color: #${finalColor}`;
  })
});
    <!-- this is only a example -->
    <table>
        <tr>
            <!-- I am creating multiple elements with the same bg color to test javascript  -->
            <td style="background-color: #ffc">FFC</td>
            <td style="background-color: #ffc">FFC</td>
            <td style="background-color: #ffc">FFC</td>

            <td style="background-color: #fb9">FB9</td>
            <td style="background-color: #fb9">FB9</td>
            <td style="background-color: #fb9">FB9</td>

            <td style="background-color: #bfc">BFC</td>
            <td style="background-color: #bfc">BFC</td>
            <td style="background-color: #bfc">BFC</td>

            <td style="background-color: #fcc">FCC</td>
            <td style="background-color: #fcc">FCC</td>
            <td style="background-color: #fcc">FCC</td>
        </tr>
    </table>

there is a pseudo-class in CSS called :is that makes you delete the code repetition

td:is([style*="background-color: #ffc"], [style*="background-color: #fb9"], [style*="background-color: #bfc"], [style*="background-color: #fcc"]) {
    background-color: #0d1117 !important;
}

documentation: https://developer.mozilla.org/en-US/docs/Web/CSS/:is


or you can use javascript by using forEach!

how it works:
you have two variables,
the first needs an array of hex color codes.
the second variable needs the final color you want the element to be colored.

how to make it work on my project:

  1. create a <script> tag or a javascript file.
  2. insert this code inside it:
let colorsArray = ["ffc", "fb9", "bfc", "fcc"]; // change this line
let finalColor = "0d1117";

colorsArray.forEach((color) => {
    let tdWithColor = document.querySelectorAll(`td[style*="background-color: #${color}"]`);

    tdWithColor.forEach((element) => {
        element.style = `background-color: #${finalColor}`;
    })
});

// watch the code snippet below if you want comments
  1. change the variables to what you need.

code snippet with demo example:

let colorsArray = ["ffc", "fb9", "bfc", "fcc"]; // change this line... don't put "#" in the beginning (js automatically adding it)
let finalColor = "0d1117"; // your final color, don't put "#" in the beginning (js automatically adding it)

/* Don't change the lines below */

/* for every color we are adding this logic below */
colorsArray.forEach((color) => {
  /* getting all the html elements with the same color */
  let tdWithColor = document.querySelectorAll(`td[style*="background-color: #${color}"]`);

  /* if there is more than one td of the same color we are coloring it too */
  tdWithColor.forEach((element) => {
    element.style = `background-color: #${finalColor}`;
  })
});
    <!-- this is only a example -->
    <table>
        <tr>
            <!-- I am creating multiple elements with the same bg color to test javascript  -->
            <td style="background-color: #ffc">FFC</td>
            <td style="background-color: #ffc">FFC</td>
            <td style="background-color: #ffc">FFC</td>

            <td style="background-color: #fb9">FB9</td>
            <td style="background-color: #fb9">FB9</td>
            <td style="background-color: #fb9">FB9</td>

            <td style="background-color: #bfc">BFC</td>
            <td style="background-color: #bfc">BFC</td>
            <td style="background-color: #bfc">BFC</td>

            <td style="background-color: #fcc">FCC</td>
            <td style="background-color: #fcc">FCC</td>
            <td style="background-color: #fcc">FCC</td>
        </tr>
    </table>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文