简体   繁体   中英

How to find a class with certain background color in jquery?

<div class="common" style="bacground-color:rgb(255,255,255)"></div>
<div class="common" style="bacground-color:rgb(140,255,255)"></div>

here i have to find class common whose background color is rgb(255,255,255).

$(".common").each(function(){
if($(this).css("bacground-color") == "rgb(255,255,255)"){
alert ('this')
}
});

This is correct method ?

Please help .

You can use CSS selector .common[style~="bacground-color:rgb(255,255,255)"] ( ~= will select attribute containing the following) here is example in jQuery:

 $(function() { console.log($('.common[style~="bacground-color:rgb(255,255,255)"]').text()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="common" style="bacground-color:rgb(255,255,255)">1</div> <div class="common" style="bacground-color:rgb(140,255,255)">2</div> 

Try this its working you can check here https://jsfiddle.net/surendra786/dt3r7x97/ you have mistaken in background spelling 'bacground' and also give the spaces b/w color code like this rgb(255, 255, 255)

$(".common").each(function(){
 if($(this).css("background-color") == "rgb(255, 255, 255)"){
  alert ('this')
}

});

plz up if its working

First, I think bacground-color is misspelled.

It should be:

<div class="common" style="background-color:rgb(255,255,255)"></div>

Second the css selector is backgroundColor.

$('.common').each(function(i, e) 
{     
   if ( $(e).css("backgroundColor")=="rgb(255, 255, 255)" )
    {
        alert(e);
    }
});
jQuery('div').each(function(){
    var a = jQuery(this).css('background-color');

    if ( a.indexOf('255, 255, 255') > -1 ){ //spaces are important (255,255,255) won't work.

        console.log('bbb')
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM