javascript - 在浏览器控制台中使用 JavaScript 将颜色代码更改为另一个颜色代码

标签 javascript css browser colors browser-console

我是一名网页设计师。

当我浏览网站时,我喜欢更改一些颜色,并查看当我更改主要颜色时网站的外观。

我使用“检查元素”来做到这一点。但这是非常耗时的工作,因为我需要添加很多代码来更改所有地方。

是否有任何 JavaScript 代码可用于使用浏览器控制台将一种颜色代码更改为另一种颜色代码。

基本上我想要的是下面这样的东西......

使用浏览器控制台将本网站所有位置的#FFF8DC 颜色更改为#e6dfc6 颜色。

最佳答案

原则

正如 Kaiido 评论的那样:.getComputedStyle()应始终以 rgb(nnn, nnn, nnn)rgba(nnn, nnn, nnn, n) 格式返回计算值。

因此,在遍历所有元素计算样式属性并替换适用的颜色值之后,应该没有太多工作要做。

我的更新

  • 因为您希望能够“将#FFF8DC 颜色更改为#e6dfc6 颜色”,我修改了此solution 中的函数( RGB to hex and hex to RGB ) 以便能够在我的代码片段中使用它,
  • 修改了我的函数,使其可以处理包含多种颜色(例如渐变)的属性值,
  • 添加了 strict 作为可选参数,以避免在包含多种颜色的值中进行颜色替换。

片段

// Below function modified from solution here: https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
function hexToRgb(hex) {
  // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function(m, r, g, b) {
    return r + r + g + g + b + b;
  });

  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? "rgb(" + [
    parseInt(result[1], 16),
    parseInt(result[2], 16),
    parseInt(result[3], 16)
  ].join(', ') + ")" : null;
}

// Function to change a color to another one
function colorChange(colorOld, colorNew, strict = false) {
  // If hex notation, convert to rgb
  if (colorOld.includes('#'))
    colorOld = hexToRgb(colorOld);
  // Loop through all elements styles
  [...document.all].forEach(elm => {
    let cStyle = getComputedStyle(elm);
    [...cStyle].forEach(prop => {
      // Escape if not a string
      if (typeof cStyle[prop] !== 'string') return;
      // Check if colorOld is in property
      if (cStyle[prop].includes(colorOld)){
        // If strict, colorOld is replaced only if it's the only value of the property
        if (!strict || cStyle[prop] === colorOld)
          elm.style[prop] = cStyle[prop].replace(colorOld, colorNew); // Replace color
      }
    })
  })
};

// Then, call your function the way you like !
colorChange("rgb(255, 0, 0)", 'orange');
colorChange("#00ff00", '#125689', true); // Note the use of the “strict” parameter here
colorChange("#00f", 'rgb(255, 0, 128)');
<p style="color: rgb(255, 0, 0);">I was red !</p>
<p style="color: #00ff00;">I was green !</p>
<p style="color: #00f;">I was blue !</p>
<div style="background: linear-gradient(to right, #f00, #0000ff);">
  <p>I was a gradient from red to blue</p>
</div>
<div style="background: linear-gradient(to right, #ff0000, #0f0);">
  <p>I was a gradient from red to green (green is not replaced here, because of the use of “strict”)</p>
</div>

当然,您可以在将这些功能复制/粘贴到您的控制台后尝试此页面上的功能。 (例如 colorChange("#eff0f1", "#ffaaaa");)

关于javascript - 在浏览器控制台中使用 JavaScript 将颜色代码更改为另一个颜色代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51760350/

相关文章:

javascript - 使用 CSS3 和 Javascript 创建一个硬币动画,就像神庙逃亡中的动画一样

javascript - 声明变量是否会产生开销?

javascript - 获取外部脚本的源代码?

html - div 之后的 div 不能按预期工作

列高和宽不等的 CSS 网格

javascript - 是否有可能在浏览器中发现类型化数组分配限制?

javascript - 如何在服务器端的假浏览器中运行操作?

javascript - 如何用逗号替换 <p> 标签

html - 将标题中的文本和图标保持在一行中

css - 强制浏览器将元素显示为文本