我想span在div其CSS包含特定背景色的情况下选择一堆。我该如何实现?
span
div
如果我正确理解问题,则选择器[attribute=value] 将不起作用, 因为其中<span>不包含“ background- color”属性。您可以快速进行测试以确认它不匹配任何内容:
[attribute=value]
<span>
$('#someDiv span[background-color]').size(); // returns 0
给出:
.one, .two { background-color: black; } .three { background-color: red; }
这里有一个片段, 将工作 :
$('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 .one, .two { background-color: black; } .three { background-color: red; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <div id="someDiv"> <span class="one">test one</span> <span class="two">test two</span> <span class="three">test three</span> </div>