Question

Je souhaite sélectionner un groupe de span s dans un div dont le CSS contient une couleur de fond particulière. Comment y parvenir?

Était-ce utile?

La solution

Si je comprends bien la question, le sélecteur [attribute=value] ne fonctionnera pas car <span> ne contient pas d'attribut & "; couleur d'arrière-plan &". vous pouvez le tester rapidement pour vous assurer qu'il ne correspond à rien:

$('#someDiv span[background-color]').size(); // returns 0

donné:

.one, .two {
  background-color: black;
}

.three {
  background-color: red;
}

voici un extrait qui fonctionnera :

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

Autres conseils

Utilisez le sélecteur d'attribut [attribut = valeur] pour rechercher une certaine valeur d'attribut.

#id_of_the_div span[background-color=rgb(255,255,255)]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top