문제

나는 많은 것을 선택하고 싶습니다 spans에서 s div CSS에는 특정 배경색이 포함되어 있습니다. 이것을 어떻게 달성합니까?

도움이 되었습니까?

해결책

질문을 올바르게 이해하면 선택기입니다 [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>

다른 팁

속성 선택기 [attribute = value]를 사용하여 특정 속성 값을 찾으십시오.

#id_of_the_div span[background-color=rgb(255,255,255)]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top