Javascript 需要获取元素中的任何子元素是否具有背景

Javascript 需要获取元素中的任何子元素是否具有背景,javascript,jquery,Javascript,Jquery,我需要找出它们的任何跨度是否具有背景样式,这样我就可以从它的属性I have$(这个)中获得值。结构是: <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

我需要找出它们的任何跨度是否具有背景样式,这样我就可以从它的属性I have$(这个)中获得值。结构是:

<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

试试这个

alert($('#operative_time_limit').find('span').length);
对于具有
background color
属性的
span
,请像这样尝试

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

我不确定在您的上下文中,
$(this)
到底是什么

然而,以下方面:

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

下一行将为您提供
$(this)

您可以使用以下代码查找除白色以外的所有背景色的范围(假设白色是默认颜色)

在这里,变量
length
可用于确定有多少跨距具有背景属性

请参见工作

使用以下内容

alert(jQuery(this).children().hasClass('background')).
hasClss确定是否将任何匹配的元素分配给给定的类。
如果类被分配给元素,.hasClass()方法将返回true,即使其他类也被分配给元素,

,但它不会返回me span have color background$(这)referers to div have id operative\u time\u limit为具有背景色的元素(如
)提供一个类,我认为这将检查类而不是样式。
var length = $('#operative_time_limit').children().filter(function () {
    return $(this).css('background-color') != 'rgba(0, 0, 0, 0)' && $(this).css('background-color') != 'transparent';
}).length;
alert(jQuery(this).children().hasClass('background')).