[英]Can I search and replace colours in a CSS file when a page loads?
What I want to do is search for all instances of a particular colour within a CSS document, identified by its hex value, and replace all those instances with a new hex value. 我想做的是在CSS文档中搜索由十六进制值标识的特定颜色的所有实例,然后用新的十六进制值替换所有这些实例。 Then, after the colours have been changed, the CSS is applied to the page.
然后, 在更改颜色之后 ,将CSS应用于页面。
Really, a CSS document is just a text file, so surely there's a way to catch it before it is served to the browser and do some text based search and replace. 实际上,CSS文档只是一个文本文件,因此,肯定有一种在将其提供给浏览器之前对其进行捕获并进行一些基于文本的搜索和替换的方法。 Look for a particular HEX code and replace all instances with another.
查找特定的十六进制代码,然后用其他实例替换所有实例。
How could that be done? 那怎么办?
You can do the following: 您可以执行以下操作:
function changeColor(oldColor, newColor) {
var all = document.getElementsByTagName('*');
Array.prototype.forEach.call(all, function(element){
if (element.style.background === oldColor) {
element.style.background = newColor;
}
})
}
This way you're looping over each element on the page, checking if it has the old color, and then if so, changing it to the new color. 这样,您就可以遍历页面上的每个元素,检查它是否具有旧颜色,然后,将其更改为新颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.