Here is a solution that I will explain step by step.
First call colorReplace("#789034", "#456780"); . The first value is the target color, and the second is the replacement color.
Inside it is $('*').map(function(i, el) { will select all the tags in the DOM tree. For each element, its getComputedStyle(el) styles array is returned. You can configure the selector for faster processing (for example, $('div').map(function(i, el)) { ).
For all style attributes containing "color" (for example, background-color , -moz-outline-color , etc.), it will check if the color value is equal to your target color. If so, it will be replaced with the target color.
Colors are returned as rgba(0,0,0,0) or rgb(0,0,0) and not as #FFFFFF , therefore, for comparison, a quick conversion from rgb to hex is performed. This uses the internal rgb2hex() function.
I hope this is what you are looking for.
function colorReplace(findHexColor, replaceWith) {
.common-color { color: #789034; } .new-cls { border-color: #789034; border-width: 4px; border-style: solid; } .awesome-one { background-color: #789034; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="common-color">color</div> <div class="new-cls">border-color</div> <div class="awesome-one">background-color</div>
source share