我想在span中选择一堆div s,其CSS包含特定的背景颜色。我如何实现这一目标?

有帮助吗?

解决方案

如果我正确理解了问题,则选择器[attribute=value] 将无法正常工作,因为<span>不包含属性<!> quot; background-color <!>“;你可以快速测试一下,确认它不匹配任何东西:

$('#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>

其他提示

使用属性选择器[attribute = value]查找某个属性值。

#id_of_the_div span[background-color=rgb(255,255,255)]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top