2013-08-06 25 views
1

我需要找出他們的任何跨度具有背景風格,所以我可以從它的屬性獲得價值我有$(this)。結構是:需要得到的是一個元素內的任何孩子有背景

<div id="operative_time_limit" class="timedivd"> 
<span title="1 hours" data-y="1" data-x="0"></span> 
<span title="2 hours" data-y="2" data-x="0"></span> 
<span title="3 hours" data-y="3" data-x="0"></span> 
<span title="4 hours" data-y="4" data-x="0"></span> 
</div> 

使用alert(jQuery(this).children().css('background').length);但總是得到0的結果

+1

給出具有類似於

回答

0

試試這個,

alert($('#operative_time_limit').find('span').length); 

對於span具有background-color財產再試試它像,

var c=0; 
$('#operative_time_limit').find('span').each(function(){ 
    if($(this).css('backgroundColor')) // or 'background-color' 
     c++; 
}); 
alert(c); 
+0

的背景色的元素的類,但是它不會返回我跨度有顏色背景 –

0

我不確定究竟是什麼在你的上下文中。

但是以下幾點:

var count = 0; 
$('#operative_time_limit span').each(function(index){ 
    if($(this).css('background')){ 
     count++; 
    } 
}); 
alert(count); 

會做到這一點。從你的問題中進一步推斷,假設你想從css背景的跨度中提取一些屬性(我們稱之爲someAttr),並且只有一個這樣的跨度。假設這些都是正確的假設,下面會給你想要的東西:

var attributeValue; 
$('#operative_time_limit span').each(function(index){ 
    if($(this).css('background')){ 
     attributeValue = $(this).attr('someAttr'); 
    } 
}); 
You now have your desired value in attributeValue 
+0

$(this)推薦人div有id operative_time_limit –

0

下面一行會給你孩子的$(this)

alert(jQuery(this).children().css('background').length); 

長度您可以使用下面的代碼查找所有跨度的有白色以外的背景顏色(白色假設是默認的顏色)

var length = $('#operative_time_limit').children().filter(function() { 
    return $(this).css('background-color') != 'rgba(0, 0, 0, 0)' && $(this).css('background-color') != 'transparent'; 
}).length; 

這裏,可變length可以用來確定有多少跨距具有背景屬性

參見工作fiddle

-1

使用以下

alert(jQuery(this).children().hasClass('background')). 

hasClss確定是否任何一個匹配元素被分配給定的類。 如果將類指定給某個元素,則.hasClass()方法將返回true,即使其他類也是

+1

我認爲這將檢查類,而不是風格。 –

相關問題