选择具有特定背景颜色的元素
我想在div
选择一堆span
,其CSS包含特定的背景颜色。 我该如何实现这一目标?
如果我正确理解了问题,则选择器[attribute=value]
将不起作用,因为不包含属性“background-color”。 你可以快速测试,以确认它不匹配任何东西:
$('#someDiv span[background-color]').size(); // returns 0
给定:
// css .one, .two { background-color: black; } .three { background-color: red; } // html test one test two test three
这是一个可行的片段:
$('div#someDiv span').filter(function() { var match = 'rgb(0, 0, 0)'; // match background-color: black /* true = keep this element in our wrapped set false = remove this element from our wrapped set */ return ( $(this).css('background-color') == match ); }).css('background-color', 'green'); // change background color of all black spans
使用属性selector [attribute = value]查找特定属性值。
#id_of_the_div span[background-color=rgb(255,255,255)]