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 を取得しています

4

4 に答える 4

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);
于 2013-08-06T07:53:15.217 に答える
0

$(this)あなたの文脈に正確に何があるかわかりません。

ただし、次のとおりです。

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

やります。あなたの質問からさらに外挿して、cssの背景を持つスパンからいくつかの属性を抽出したいと仮定し(それを と呼びましょうsomeAttr)、そのようなスパンは1つしかありません。これらが正しい仮定であると仮定すると、次のようにすると、必要なものが得られます。

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
于 2013-08-06T07:56:12.173 に答える
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を使用して、背景プロパティを持つスパンの数を決定できます

作業フィドルを参照してください

于 2013-08-06T07:58:58.917 に答える
-1

以下を使用

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

hasClss は、一致した要素のいずれかに特定のクラスが割り当てられているかどうかを判断します。.hasClass() メソッドは、クラスが要素に割り当てられている場合、他のクラスも割り当てられている場合でも true を返します。

于 2013-08-06T08:01:54.580 に答える