2017-07-01 17 views
0
<div class="common" style="bacground-color:rgb(255,255,255)"></div> 
<div class="common" style="bacground-color:rgb(140,255,255)"></div> 

這裏我必須找到類的常見的背景顏色是rgb(255,255,255)。如何在jQuery中找到具有某種背景色的類?

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

這是正確的方法嗎?

請幫忙。

+0

到目前爲止,你已經嘗試過什麼? –

+2

將內聯CSS移入CSS類並按類名查找 – dfsq

+0

.common [background-color =「rgb(255,255,255)」] –

回答

1

試試這個它的工作 您可以在這裏查看https://jsfiddle.net/surendra786/dt3r7x97/ 你誤會了在後臺拼寫 'bacground',也給了空間的B/W這樣的RGB(255,255,255)

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

});

PLZ了,如果它的工作

+0

ok :)。這是工作 。 –

1

您可以使用CSS選擇器.common[style~="bacground-color:rgb(255,255,255)"]~=將選擇包含下列屬性)這裏是jQuery的例子:

$(function() { 
 
\t 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>

0

首先,我認爲bacground色的拼寫錯誤。

它應該是:

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

二的CSS選擇器的backgroundColor。

$('.common').each(function(i, e) 
{  
    if ($(e).css("backgroundColor")=="rgb(255, 255, 255)") 
    { 
     alert(e); 
    } 
}); 
0
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') 
    } 
}) 
相關問題