简体   繁体   English

对所有基于background-color的元素使用jQuery过滤器

[英]Using jQuery filter on all elements based on background-color

I'm currently using a generated html code. 我目前正在使用生成的html代码。

I'm trying to select all elements and then filter those who got the same background-color and change it. 我试图选择所有元素,然后过滤那些具有相同背景色的元素并进行更改。 My current code, which is not working: 我当前的代码不起作用:

$('*').filter(function(){
   return $(this).css('background-color') === '#0689cb';
}).css('background-color', 'green');

It returns a lot of elements, but it doesn't change anything. 它返回很多元素,但没有任何改变。

If I try the .length like this: 如果我这样尝试.length:

$('*').filter(function(){
   return $(this).css('background-color') === '#0689cb';
}).length;

Will return 0, which means, it is not getting anything. 将返回0,这意味着它什么也没有得到。

What am I doing wrong with my filter function? 我的过滤器功能出了什么问题?

Oh, and I tried the selector with $('div') to see if it was something wrong with the selector, but it didn't change anything. 哦,我尝试用$('div')选择器,看选择器是否有问题,但没有改变。

Thanks for the help. 谢谢您的帮助。

background-color will return rgb . background-color将返回rgb You'll need to find the RGB value of your the hex code and use that in your conditional. 您需要找到十六进制代码的RGB值并将其用于条件代码中。

$('*').filter(function() {
   return $(this).css('background-color') === 'rgb(6, 137, 203)';
}).css('background-color', 'green');

Reference this post if you decide to manually convert the hex value: 如果您决定手动转换十六进制值,请参考此文章

You need to use an RGB value and you need to write the values with commas and spaces between them. 您需要使用RGB值,并且需要使用逗号和空格写入值。

Below is a function (adapted from here ) that will convert valid hex codes to the correct RGB string. 下面是一个函数(从此处改编),它将有效的十六进制代码转换为正确的RGB字符串。

 $('*').filter(function(){ return $(this).css('background-color') === hexToRgb('#0689cb'); }).css('background-color', 'green'); function hexToRgb(hex) { var result = /^#?([af\\d]{2})([af\\d]{2})([af\\d]{2})$/i.exec(hex); // Correct string should be like: "rgb(6, 137, 203)" <-- note the spaces return "rgb(" + parseInt(result[1], 16) + ", " + parseInt(result[2], 16) + ", " + parseInt(result[3], 16) + ")"; } 
 .colorToFind { background-color:#0689cb; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="colorToFind">hello</div> <div>hello</div> <div class="colorToFind">hello</div> <div>hello</div> 

Simple way to match your hex color to color that computed style returns (which will not be hex) is to create an element...apply that color to it using hex, then see what browser returns as computed color 将十六进制颜色与计算样式返回的颜色(不会是十六进制)进行匹配的简单方法是创建一个元素...使用十六进制将该颜色应用于它,然后查看浏览器返回的计算颜色

 function getComputedColorVal(hex) { return $('<span>').css('color', hex).css('color'); } let hex = '#0689cb', computedColor = getComputedColorVal(hex) $('*').filter(function() { return $(this).css('background-color') === computedColor; }).css('background-color', 'green'); 
 .blue{background-color:#0689cb} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <ul> <li class="blue">Item</li> <li>Item</li> <li class="blue">Item</li> <li>Item</li> <li class="blue">Item</li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM